in reply to a newbie's output reversed
The quick ways to do what I think you are doing are to use either pack and unpack or printf/sprintf. Something more analogous to your method can be done using unshift to build an array of ones and zeros depending on whether the decimal value leaves a remainder when divided by two then shifting it right one bit, doing this eight times then finally joining the array into a string representing the binary value.
knoppix@Microknoppix:~$ perl -E ' > $dec = 167; > say unpack q{B*}, pack q{c}, $dec; > printf qq{%08b\n}, $dec; > unshift @bits, do { > my $bit = $dec % 2 ? 1 : 0; > $dec >>= 1; > $bit; > } for 0 .. 7; > say join q{}, @bits;' 10100111 10100111 10100111 knoppix@Microknoppix:~$
I hope this is helpful.
Cheers,
JohnGG
|
|---|