Jump to content
KenHorse

Extracting nibbles from byte not working

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?)

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
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.

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


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