in reply to convert decimal to binary with unpack problem
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';
|
|---|