PDA

View Full Version : Decimal -> Binair


chrisvmarle
08-13-2002, 11:33 PM
Does anyone know a good way to convert a decimal number to the binair-system?

(I'd prefer a sub, returning the binair value)

Thanks in advance.

Mzzl, Chris

pager
08-14-2002, 12:52 AM
Lol...

$myinput=70323;
vec($number,0,32)=$myinput;
$bitstring=unpack("B*", $number);
print $bitstring;


Set the 32 number to the number of bits you actually need if you don't want preceeding zeros - but it must be a binary power of two to be valid, ie, 1,2,4,8,16,32 (maximum).

chrisvmarle
08-14-2002, 08:09 AM
Thanks man, the code works.

But I'd like to understand it too...
I'm not quiet a newB to Perl, but I don't understand the code, could you explain it for me?

Thanks again.

Mzzl, Chris

pager
08-14-2002, 08:15 AM
I'll have it figured out as soon as I break out the Perl source code for the VEC function!

Honestly I don't have a single clue as to how the VEC function works... Out of three really good books, the longest mention of it is less than half a page.

I just lucked out in finding it.... Credit the books for this one!