Jump to content

bytes of numbers


Michdd

Recommended Posts

I need a way to get the bytes that represent a numerical data-type's value (int, short, char, whatever). And vise versa.

 

For example:

 

265 in binary would be:

 

00000001 00001001

 

so the bytes would be 1 and 8, then I'd need to be able to convert 1, 8 back to 265.

 

I'm sure I could do this myself, but I'm just looking for the easiest way to do this. I feel like I should know this, and it's really simple. But I'm extremely exhausted so please forgive me if this is a stupid question.

Link to comment
https://forums.phpfreaks.com/topic/189117-bytes-of-numbers/
Share on other sites

I knew about that function. But I was just being stupid. After sleeping I was able to come up with a solution. This is what I came up with (though, I'm pretty sure the first function can be simplified)

 

function getBytes($number)
{
$binary = decbin($number);
for($i = 0, $l = strlen($binary);$i < $l;$i += 
{
	$lim = ($extra = strlen($binary) %  == 0 ? 8 : $extra;
	$arr[] = substr($binary, 0, $lim);
	$binary = substr($binary, $lim);
}
return array_map('chr', array_map('bindec', $arr));
}

function getNumber($bytes)
{
$bytes = array_map(
	create_function('$x',
					'return str_pad(decbin(ord($x)), 8, "0", STR_PAD_LEFT);'
					),
	$bytes
);
return bindec(implode($bytes));
}

 

Say you pass the number 257. The number 257 in binary is 00000001 00000001. So it puts each of them into a separate byte. Coming up with the bytes: 1 & 1. Then when transforming that back into the numerical value the other function takes the binary represented by each byte, combines it and turns it back into a decimal.

 

Does anyone have any suggestions or a reason why I shouldn't do it like this?

 

One other question, this obviously won't work for negative values. Now, should I implement a way to convert negative values correctly? Or is this not really required? If you haven't guessed yet this is for sending data over a socket. And I can't think of any specific case myself where a well-designed program would actually require this.

Link to comment
https://forums.phpfreaks.com/topic/189117-bytes-of-numbers/#findComment-999171
Share on other sites

  Quote

One other question, this obviously won't work for negative values. Now, should I implement a way to convert negative values correctly? Or is this not really required? If you haven't guessed yet this is for sending data over a socket. And I can't think of any specific case myself where a well-designed program would actually require this.

 

You typically use "two's complement" for representing negative integers in binary. Go look it up. It's fairly easy though. To flip the sign you just flip all bits and say +1.

 

So if you have +3 using 8 bits:

00000011

Flip all bits:

11111100

Add 1 and you now have -3:

11111101

Now flip all the bits:

00000010

And add 1 and you're back at +3:

00000011

Link to comment
https://forums.phpfreaks.com/topic/189117-bytes-of-numbers/#findComment-999291
Share on other sites

  Quote

  Quote

...

Add 1 and you now have -3:

11111101

...

But that's also the binary representation for 253, how can you tell the difference?

 

You cannot represent 253 with a signed 8-bit int. Half the range goes to the negative integers so the max number is 2^7-1=127.

Link to comment
https://forums.phpfreaks.com/topic/189117-bytes-of-numbers/#findComment-999674
Share on other sites

Doh, that should have been pretty obvious. Thanks.

 

@salathe: That's great, and I'll probably use that for the PHP part of this project. Unfortunately, something so convenient isn't available in the other programming language I'll also be using. I'll just write some procedures that do the same thing as pack() on that platform using the help I got int his topic.

Link to comment
https://forums.phpfreaks.com/topic/189117-bytes-of-numbers/#findComment-999678
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.