KenHorse Posted June 16, 2017 Share Posted June 16, 2017 Take the following code that reads in the following lines from a text file:1,1612,1693,14,05,117Each value after the comma is a byte that contains 2 bytes (with a value of 0 - 15) that were stored by shifting a 1st byte to the left. You get the idea... Code: Select all $myfile = fopen("test.mem", "r") or die("Unable to open file!"); $substring =""; for($b = 1; $b <=1; $b++){ $indata = fgets($myfile); $position = strpos($indata,","); $sub = intval(substr($indata, $position + 1)); $low = $sub & 0x0F; $high = ($sub & 0xF0) >> 4; $sub = $high . $low; if($sub == "0"){ $b = 5 - $b; for($c = 1; $c <= $b; $c++){$indata = fgets($myfile);} break; } if($b < 5){$substring = $substring . $sub;} chop($substring); } echo "Result " . " $substring\r\n"; The values in the file should result in "10901" but the above code results in 10110901.I KNOW the values in the file are stored correctly as I can correctly extract the info in another language but I can't seem to make it work in PHPWhat am I missing here (integer versus byte thing?) Quote Link to comment Share on other sites More sharing options...
requinix Posted June 16, 2017 Share Posted June 16, 2017 I can't figure how you're supposed to get 10901 from that, and your misuse of the term "byte" isn't helping. You have it working in another language? What's that code? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 16, 2017 Share Posted June 16, 2017 It's a text file. That means that "1,161" is comprised of 5 bytes, they being: 1 , 1 6 1 So - what you are saying is just not correct. Care to explain what you really want in plain English? Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 17, 2017 Share Posted June 17, 2017 A byte is system defined, however in most systems, it's an 8 bit value. Unless using unicode, each byte represents a single character. So I think this was the point being made by ginerjm. Nibbles are 4 bit portions of an 8 bit byte, with the high nibble being the left 4 bits and low order being the right 4 bits. PHP does not have byte values, but they do have integers. You can do bit arithmetic with php. In looking at your file it's unclear what this line means: Each value after the comma is a byte that contains 2 bytes (with a value of 0 - 15) that were stored by shifting a 1st byte to the left. You get the idea... Not really. A byte can't contain 2 bytes. You need to explain this better. An unsigned byte can have a value between 0-255. Often, hexadecimal is used to represent the values in a single byte because you can represent all the values from 0-255 using 2 hex digits. When you start talking about 0-15 it makes me think you are talking about hex, since the values of hex representation are from 0-15 (0-F). In terms of bit shifting, when you shift bits you are either losing them (shifting to the right) or increasing the number by a power of 2 when left shifting. Since a php integer is larger than a byte, if you assume you will shift off (lose bits) by left shifting, that won't happen with small values like the ones in your file. Last but not least you have weird stuff like a loop that runs once: for($b = 1; $b <=1; $b++) { This makes it hard to understand what the code is supposed to be doing. 1 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.