Michdd Posted January 20, 2010 Share Posted January 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/189117-bytes-of-numbers/ Share on other sites More sharing options...
Daniel0 Posted January 20, 2010 Share Posted January 20, 2010 Can't you just use bindec? Quote Link to comment https://forums.phpfreaks.com/topic/189117-bytes-of-numbers/#findComment-998559 Share on other sites More sharing options...
Michdd Posted January 21, 2010 Author Share Posted January 21, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/189117-bytes-of-numbers/#findComment-999171 Share on other sites More sharing options...
Daniel0 Posted January 21, 2010 Share Posted January 21, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/189117-bytes-of-numbers/#findComment-999291 Share on other sites More sharing options...
salathe Posted January 21, 2010 Share Posted January 21, 2010 Maybe I'm missing the point (it happens occasionally) but couldn't you just pack whatever you want into binary? Quote Link to comment https://forums.phpfreaks.com/topic/189117-bytes-of-numbers/#findComment-999293 Share on other sites More sharing options...
Michdd Posted January 22, 2010 Author Share Posted January 22, 2010 ... Add 1 and you now have -3: 11111101 ... But that's also the binary representation for 253, how can you tell the difference? Quote Link to comment https://forums.phpfreaks.com/topic/189117-bytes-of-numbers/#findComment-999669 Share on other sites More sharing options...
roopurt18 Posted January 22, 2010 Share Posted January 22, 2010 But that's also the binary representation for 253, how can you tell the difference? In 2s complement, if the most significant bit is a 1, then the number is negative. Quote Link to comment https://forums.phpfreaks.com/topic/189117-bytes-of-numbers/#findComment-999672 Share on other sites More sharing options...
Daniel0 Posted January 22, 2010 Share Posted January 22, 2010 ... 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. Quote Link to comment https://forums.phpfreaks.com/topic/189117-bytes-of-numbers/#findComment-999674 Share on other sites More sharing options...
Michdd Posted January 22, 2010 Author Share Posted January 22, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/189117-bytes-of-numbers/#findComment-999678 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.