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
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
Share on other sites

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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.