Jump to content

Extracting nibbles from byte not working


KenHorse

Recommended Posts

Take the following code that reads in the following lines from a text file:

1,161
2,169
3,1
4,0
5,117

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...
 

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 PHP

What am I missing here (integer versus byte thing?)

Link to comment
Share on other sites

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.

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.