|
Posted by Michael Fesser on July 11, 2008, 6:33 pm
Please log in for more thread options
.oO(inexion)
>can anyone help me out with a bit handler?
>
>it seems as if php's decbin() removes leading zeroes when converting
>from hex....so for example decbin(hexdec()); basically removes any
>leading zeroes.
Correct, because in a number leading zeros don't have any meaning:
1 = 01 = 001 ...
>i'm trying to convert some 32bit hex words into bin, and the first 12
>bits need to be broken down into 3-bit sets for decoding a key of
>sorts. for example:
>
>(on paper)
>0x2101769:
>-------normal bin------- ----3-bit sets----
>0010 0001 0000 0001 011 101 101 001
>
>(in php)
>decbin(hexdec(2101769)) = 1000000001001000001001
>
>
>are there any functions that could break apart the number once i get
>the conversion done correctly?
>thanks!
Pad the string with zeros to the required length of at least 12 chars,
take the last 12 chars from it and split them into groups of three:
<?php
$hexCode = '2101769';
$binCode = substr(sprintf('%012b', hexdec($hexCode)), -12);
$parts = str_split($binCode, 3);
var_dump($parts);
?>
HTH
Micha
|