true has asked for the wisdom of the Perl Monks concerning the following question:

howdy monks.
I'm have trouble converting a decimal value of 2100 to the binary form of 0011 0100
When i use
print unpack("B*",2100)."\n";
I get 00110010001100010011000000110000
and when i use the hex of 2100 (34),
print unpack("B*",34)."\n";
i get 0011001100110100
but i need to retrieve the bin value of '0011 0100'
Can someone guide me in the form of a one-liner?
2100 = '0011 0100';

WHY??
I'm generating a .swf file from perl. A vector rectangle has four points stored in the .swf file. These values have to be converted into binary then layed together side by side, and finally packed into the swf file. Because these shifted values are spread across the entire file, i have to convert them to binary, then translated their value.
Also note. 2100 = '0011 0100' is the result i get from using my Dimdow's calculator. I'm trying to mimic that conversion in perl. thanks for reading.

Replies are listed 'Best First'.
Re: convert decimal to binary with unpack problem
by pfaut (Priest) on Jan 16, 2003 at 19:22 UTC

    unpack looks at the scalar passed to it as a string. What you were unpacking was "2100". If you look at what you got back and convert it to hex, you will see the ascii codes for '2' (00110010), '1' (00110001), '0' (00110000), '0' (00110000). In your second try, you got the ascii codes for '3' (00110011) and '4' (00110100).

    If you want to use pack/unpack to perform these conversions, you have to give it binary data. You can build this with pack as follows.

    # Create a buffer containing the binary representation # of the number we want converted my $packed = pack("n", 2100); # unpack to binary my $bin = unpack("B*", $packed); print "$bin\n";

    This displays 0000100000110100.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: convert decimal to binary with unpack problem
by thinker (Parson) on Jan 16, 2003 at 19:24 UTC
    Hi true,

    You can use the following to do what you want.
    ($n=unpack("B32", pack("N", 2100)))=~s/\d*(\d{8})/$1/; print $n."\n";

    Hope this helps

    thinker
Re: convert decimal to binary with unpack problem
by Mr. Muskrat (Canon) on Jan 16, 2003 at 19:32 UTC
    Part of your problem is that you are assuming that 00110100 is the binary equivalent of 2100 (hex 834). It is the binary equivalent of 52 (hex 34). Sounds like your calculator is broken (:

    quick and dirty hack: print unpack("B*",chr(52)),$/;

    fixed typo

Re: convert decimal to binary with unpack problem
by BrowserUk (Patriarch) on Jan 16, 2003 at 19:34 UTC

    Erm? 0x34 = 0b0011 0100 = 52 decimal. Where does the 2100 come from?

    You might also look at printf and sprintf format '%b' for converting decimal to hex.


    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Re: convert decimal to binary with unpack problem
by true (Pilgrim) on Jan 16, 2003 at 19:56 UTC
    yikes! your comments have solved my problem. i was in fact trying to retrieve 100000110100 for 2100. The way the .swf file is stacked side-to-side in binary made it difficult to see the error. Packing then unpacking works great.
    print unpack("B*", pack("n", 2100))."\n";
    gives me the correct answer '100000110100'
    Thanks everybody.

    jtrue