Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8354365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:31:40+00:00 2026-06-09T09:31:40+00:00

I am tasked with converting some Fortran code to PHP and am stumbling at

  • 0

I am tasked with converting some Fortran code to PHP and am stumbling at the last hurdle.

In essence the Fortran converts a REAL into a binary CHAR(4) which it ultimately writes to file.

The Fortran (which also confuses me) is as follows:

FUNCTION MKS(x)
    CHARACTER (LEN=4) :: MKS             ! The 4-character string which is returned to 
    REAL :: x                            ! The incoming single-precision variable 
    CHARACTER (LEN=1), DIMENSION(4) :: a ! A working variable
    CHARACTER (LEN=4) :: d               ! A working variable
    CALL MKS1(x,a)                       ! Send x - get back a(1), a(2), a(3), a(4) 
                                    ! Note: x will hold the first 32 bits referenced 
                                         ! and a will hold the next 32 bits
d = a(1) // a(2) // a(3) // a(4)     ! concatenate into 1 string (d)
MKS = d                              ! assign string to variable MKS
END FUNCTION MKS

SUBROUTINE MKS1 (b,c)
    IMPLICIT NONE
    CHARACTER (LEN=1), DIMENSION(4)  :: b    ! array with incoming 32 bits
    CHARACTER (LEN=1), DIMENSION(4)  :: c    ! array with each character returned
    INTEGER :: i                             !  DO Loop counter
    DO i=1,4
        c(i) = b(i)
    END DO
END SUBROUTINE MKS1

I have attempted to recreate this function using php as follows

pack('CCCC', $value & 0x000F, 
           ($value>>8) & 0x000F, 
           ($value>>16) & 0x000F, 
           ($value>>24) &0x000F);

But on comparing the output values using the *nix od command shows completely different results.

What is the correct way to pack the equivalent to a Fortran REAL into a char[4] Array in PHP?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:31:42+00:00Added an answer on June 9, 2026 at 9:31 am

    It turned out to be quite simple.

    Your FORTAN REAL is stored as an IEEE 754 32 bit floating point number.

    The output from your od was misleading. Converting it to hex gives the following.

    0115040 0134631 0005077
    
    0x20, 0x9A, 0x99, 0xB9, 0x3f, 0x0a
    

    The first and last bytes of the file are redundant, they are a space and a carriage return respectively. The bit we’re after is the middle 4 bytes.

    Using pack we can convert from floats (warning – endianness is machine dependant).

    The following:

    var_dump(bin2hex(pack('f', 1.450)));
    

    Gives us a familar sequence of bytes.

    string(8) "9a99b93f"
    

    So instead of converting to hex, output that to a file with a space at the start and a carriage return at the end, and you’ll have an identical file. (as long as your PHP/machine configuration doesn’t do something mad with floats – but even then if you follow the IEEE 754 spec, you should be able to reproduce it)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been tasked with converting several php classes into java classes, which is
So I've been tasked with converting some T-SQL code to C code. Whoever wrote
I am tasked with converting some Python code to Java. I have some experience
I have been tasked with converting some scripts referencing an Informix database to be
I've been tasked with converting some text log files from a test reporting tool
I have a bit of code that I have been tasked with converting to
I have been tasked with converting a SQL Server database into a MySQL 5.*
I've been tasked in a homework assignment with converting a loop in C# into
Background: I've been tasked with converting an Access database application to ASP.Net C# MVC.
I've been tasked when integrating a web form into Oracle CRM on Demand (Siebel)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.