Using unpack for bits

Using unpack for bits

am 14.08.2011 09:51:27 von th0ma5_anders0n

Hi all,

When converting a single number (eg 6) to its binary format using
unpack as in:

unpack 'B8', '6'; # output = 00110110

I get the 8 character output 00110110.
Does unpack have options for it to only return the last 4 characters
of this output (ie 0110)?
Or is the only option to use substr?

Got a little confused by the documentation :(


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Using unpack for bits

am 14.08.2011 10:43:17 von jwkrahn

th0ma5_anders0n@yahoo.com wrote:
> Hi all,

Hello,

> When converting a single number (eg 6) to its binary format using
> unpack as in:
>
> unpack 'B8', '6'; # output = 00110110

You are not converting the number 6, you are converting the string '6'.


> I get the 8 character output 00110110.
> Does unpack have options for it to only return the last 4 characters
> of this output (ie 0110)?
> Or is the only option to use substr?

$ perl -le'printf "%04b\n", 6'
0110



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Using unpack for bits

am 14.08.2011 13:00:48 von timothy adigun

--0016e6da93d02480f104aa750f93
Content-Type: text/plain; charset=ISO-8859-1

Hi th0ma5_anders0n,

if you must use unpack try these:

>>on Win32:
perl -e "printf('%04g',unpack("B8",pack('c',6)))" # Ans: 0110

>>on Ubuntu
perl -e 'printf("%04g\n",unpack("B8",pack("c",6)))' # Ans: 0110

regards.
Timothy

--0016e6da93d02480f104aa750f93--