Forum Moderators: coopster

Message Too Old, No Replies

convert byte to binary

character conversion to binary representation

         

phazei

7:27 pm on Jan 31, 2007 (gmt 0)

10+ Year Member



I'm receiving 2 bytes of data from a socket connection.
They are shown as 2 unknown characters.

What I need to do is view their binary representation.

I've been playing around with unpack, but can't seem to get anywhere. Google hasn't helped a bit.

Like, if I just had "e" how could I convert that to "01100101"?

Thanks,
Adam

eelixduppy

8:13 pm on Jan 31, 2007 (gmt 0)



You can try base_convert [us2.php.net]. It may be what you are looking for.

phazei

12:25 am on Feb 1, 2007 (gmt 0)

10+ Year Member



It turn out that the service I'm getting the data from messed up and told me the wrong port number.
So it would get the first half of the correct response, but the second half, a 2 byte number, was blank. So everything I was trying was way over done and what I had a while ago worked...

Though I did find this nifty function, that converts ascii to binary, in the process if it's ever userful to anyone: (i didn't make it nor do i remember where i found it, lots of google pages)

function asc2bin ($ascii)
{
while ( strlen($ascii) > 0 )
{
$byte = "";
$i = 0;
$byte = substr($ascii, 0, 1);
while ( $byte!= chr($i) ) { $i++; }
$byte = base_convert($i, 10, 2);
$byte = str_repeat("0", (8 - strlen($byte)) ) . $byte; /* This is an endian (architexture) specific line, you may need to alter it. */
$ascii = substr($ascii, 1);
$binary .= $byte." ";
}
return $binary;
}

jatar_k

1:25 pm on Feb 1, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what about something like pack() [php.net]