Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Handling binary

by Eily (Monsignor)
on Sep 12, 2015 at 14:29 UTC ( [id://1141777]=note: print w/replies, xml ) Need Help??


in reply to Handling binary

pack and unpack are, as a matter of fact a way to achieve what you want although it's not done in one step:
perl -e 'print join " ", map { chr(ord('A')+eval "0b$_") }  unpack "(A5)*", unpack "B*", pack "H*", "139686DA20C1"'

The first transformation is pack "H*", $str where the hexadecimal numbers are turned into binary data.

Then unpack "B*", $str; turns the binary data into a string consisting of the characters '0' and '1'.

unpack "(A5)*", $str; is one of the many ways to split a string every five characters.

Then, packing and unpacking those 5 bits strings into numbers would have been possible, but eval "0b$str" just does that in one step.
chr($nb + ord('A')) is the character at the position "position of A" + $nb. Which is what you want:)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1141777]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2024-04-18 14:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found