Using unpack for bits

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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
th0ma5_anders0n [ So, 14 August 2011 09:51 ] [ ID #2063305 ]

Re: Using unpack for bits

th0ma5_anders0n [at] 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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
jwkrahn [ So, 14 August 2011 10:43 ] [ ID #2063306 ]

Re: Using unpack for bits

--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--
timothy adigun [ So, 14 August 2011 13:00 ] [ ID #2063307 ]
Perl » gmane.comp.lang.perl.beginners » Using unpack for bits

Vorheriges Thema: bitwise and help plz
Nächstes Thema: Just why?