Jump to content

4 bytes to float


KenHorse

Recommended Posts

Take the following 4 bytes, stored in the order shown:

0, 0, 220, 66

The above bytes represent the number 11000, stored as (from the compiler I use for embedded processor work) as a "single", which they define as a "signed 32 bit binary number". I take that to be the same as a float?

How can I convert these 4 bytes to their actual value of 11000? (I've tried pack with no success)

Link to comment
Share on other sites

No, it is not the same - read up on what "single" and "double" are. However sometimes people say "float" when they really mean "single".

 

Are you sure that's 11000 and not, say, 110? Because I think it's supposed to be 110.

 

And pack() is the wrong one. That encodes to binary. You want unpack() to decode from binary. Probably using 'g'.

Link to comment
Share on other sites

I have to agree with Requinix, something doesn't add up here. Let's assume that this number is as you described. It would be reasonable to surmise that it's in IEEE 754 format.

 

Given your byte series, here's some code to test:

 

// Bytes of 0,0,220,66
$bin = "\x00\x00\xDC\x42";
$a = unpack('f', $bin);
echo $a[1]; // 110

 

See https://3v4l.org/Mp2Fu

Link to comment
Share on other sites

Sorry, yes..the value in those bytes should represent 110.0 (sorry, somewhat new to PHP and I inherited a file that I'm working my way though. Obviously I missed a few things along the way....

 

Those 4 bytes are actually read in - line by line -  from a file. This file contains roughly 4,000 lines and format is:

 

1,0

2,0

3,220

4,66

 

So and so forth...

 

Here is how I'm reading (and processing) this file

<?php

$myfile = fopen("myfile.dat", "r") or die("Unable to open file!");

while(!feof($myfile)) {

$indata = fgets($myfile);	
	$position = strpos($indata,","); 
	$buffer1 = intval(trim(substr($indata, $position + 1)));	
	$indata = fgets($myfile);	
	$position = strpos($indata,","); 
	$buffer2 = intval(trim(substr($indata, $position + 1)));	
	$indata = fgets($myfile);	
	$position = strpos($indata,","); 
	$buffer3 = intval(trim(substr($indata, $position + 1)));	
	$indata = fgets($myfile);	
	$position = strpos($indata,","); 
	$buffer4 = intval(trim(substr($indata, $position + 1)));

}
?>
Link to comment
Share on other sites

Um, what?

 

Your original example had a comma-separated list of four numbers per line. Now you suddenly have four lines with two numbers each.

 

I think we had more than enough confusion. What is the actual format? Not the one you've made up for this forum. The real file format.

Link to comment
Share on other sites

The file contains 4,096 lines, each one representing 1 byte of data in the EEPROM of an embedded processor. The format of the file is the memory location in decimal, a comma and the value of that EEPROM location. The actual locations of the 4 bytes in question are below but that really shouldn't matter to this issue

 

283,0

284,0

285,220

286,66

Link to comment
Share on other sites

Here's a more modern, simpler and cleaner way of approaching this, that incorporates the advice already provided

 

<?php

$file = new SplFileObject("myfile.dat");

$raw = '';
while (!$file->eof()) {
    // Get one line from the file.
    $values = explode(',', $file->fgets());
    $raw .= chr($values[1]);

    // Is this the 4th value?
    if (0 == $values % 4) {
        // Do whatever you need with the value. Just echoed it here
        echo unpack('f', $raw);
        $raw = '';
    }
}

// Unset the file to close the file handle.
$file = null;

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.