Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

nisc wwwxxx, wwwxxx0cm, should producers of software-based services, such as atms, be held liable for economic injuries suffered when their systems fail?, wwwxxx0cm, www.webdp.net, Event 9 IIS log failed to write entry, wwwxxx jeffs, Catastrophic failure Unexpected method call sequence. 0x8000ffff (-2147418113)., ksh lock a file, [unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_DBC failed

Links

XODOX
Impressum

#1: Using unpack for bits

Posted on 2011-08-14 09:51:27 by 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/

Report this message

#2: Re: Using unpack for bits

Posted on 2011-08-14 10:43:17 by 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/

Report this message

#3: Re: Using unpack for bits

Posted on 2011-08-14 13:00:48 by 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--

Report this message