KenHorse Posted June 27, 2017 Share Posted June 27, 2017 Take the following 4 bytes, stored in the order shown:0, 0, 220, 66The 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) Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 27, 2017 Share Posted June 27, 2017 What base is this number? Quote Link to comment Share on other sites More sharing options...
requinix Posted June 27, 2017 Share Posted June 27, 2017 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'. Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 27, 2017 Share Posted June 27, 2017 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 Quote Link to comment Share on other sites More sharing options...
KenHorse Posted June 28, 2017 Author Share Posted June 28, 2017 (edited) 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))); } ?> Edited June 28, 2017 by KenHorse Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted June 28, 2017 Share Posted June 28, 2017 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. Quote Link to comment Share on other sites More sharing options...
KenHorse Posted June 28, 2017 Author Share Posted June 28, 2017 (edited) 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 Edited June 28, 2017 by KenHorse Quote Link to comment Share on other sites More sharing options...
requinix Posted June 28, 2017 Share Posted June 28, 2017 So each line has a line number and the byte value? Take your four $buffer variables, convert them to characters, concatenate them all into a string in the order 1234, then unpack() with 'f'. Quote Link to comment Share on other sites More sharing options...
Solution gizmola Posted June 28, 2017 Solution Share Posted June 28, 2017 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; Quote Link to comment Share on other sites More sharing options...
KenHorse Posted June 28, 2017 Author Share Posted June 28, 2017 (edited) Thanks all who responded and especially requinix and gizmola. Both of these solutions worked (or perhaps I should say either of them do). Edited June 28, 2017 by KenHorse Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.