Click here to get back home

bit handling function

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
bit handling function inexion 07-11-2008
Posted by inexion on July 11, 2008, 1:04 pm
Please log in for more thread options
hello,

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.

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!

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

Posted by axlq on July 11, 2008, 9:08 pm
Please log in for more thread options
>it seems as if php's decbin() removes leading zeroes when converting
>from hex....so for example decbin(hexdec()); basically removes any
>leading zeroes.

If you want leading zeros in a 32-bit binary representation, try
$binary = sprintf('%032b', $number);

>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

Lowest 3 bits:
$low3 = sprintf('%03b', $number & 0x0003);

Next 3 bits:
$next3 = sprintf('%03b', ($number >> 3) & 0x0003);

Next 3 bits:
$another3 = sprintf('$03b', ($number >> 6) & 0x0003);

...and so on.

-A

Posted by inexion on July 14, 2008, 7:28 pm
Please log in for more thread options
Thanks for the replies guys -


i got it working with this code:

$tnum = array();
        $tnum[] = hexdec($targets['flags'][$i]);
        $binCode = substr(sprintf('%012b', $tnum[$i]), -12);
        $parts = str_split($binCode, 3);
        echo "flags: ".$parts[0]." ".$parts[1]." ".$parts[2]." ".
$parts[3]."<br />";

thanks!!

Posted by Michael Fesser on July 14, 2008, 8:29 pm
Please log in for more thread options
.oO(inexion)

>Thanks for the replies guys -
>
>
>i got it working with this code:
>
>$tnum = array();
>        $tnum[] = hexdec($targets['flags'][$i]);
>        $binCode = substr(sprintf('%012b', $tnum[$i]), -12);
>        $parts = str_split($binCode, 3);
>        echo "flags: ".$parts[0]." ".$parts[1]." ".$parts[2]." ".
>$parts[3]."<br />";

I would write the last line as

echo "flags: <br />";

But that's just personal preference.

>thanks!!

You're welcome.

Micha

Similar ThreadsPosted
A change in the handling of function arguments January 20, 2008, 5:16 pm
error handling January 14, 2005, 4:02 pm
Handling PDO Exceptions March 29, 2006, 8:06 am
PHP handling data April 5, 2006, 10:25 am
Semaphore handling June 28, 2006, 5:00 am
Handling Dates June 15, 2007, 6:32 pm
Image Handling October 13, 2007, 4:30 am
handling 64 bit ints in php December 28, 2007, 1:50 am
Textarea handling in php ? March 18, 2008, 6:57 am
handling forms in PHP October 14, 2008, 7:20 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap