Perl golf: printing data in hex

A

A. Farber

Given a binary string in $x, what would be the shortest way
to print it out in hex, like: 3A BC DE FF 00 .. .. .. ?
 
T

Thomas Kratz

A. Farber said:
Given a binary string in $x, what would be the shortest way
to print it out in hex, like: 3A BC DE FF 00 .. .. .. ?

my $str = "\x3A\x3A\xBC\xDE\xFF\x00";
print join ' ', map uc(unpack("H*", $_)), split(//, $str);

Thomas
 
R

Ron Parker

Given a binary string in $x, what would be the shortest way
to print it out in hex, like: 3A BC DE FF 00 .. .. .. ?

$_=unpack('H*',$x);s/(..)/\U\1 /g;print
 
F

fifo

At said:
Given a binary string in $x, what would be the shortest way
to print it out in hex, like: 3A BC DE FF 00 .. .. .. ?

print uc join' ',unpack'(H2)*',$x
 
J

Jay Tilton

(e-mail address removed) (A. Farber) wrote:

: Given a binary string in $x, what would be the shortest way
: to print it out in hex, like: 3A BC DE FF 00 .. .. .. ?

printf'%*v02X',$",$x

That was broken in 5.8.0, which upset me greatly.
It's fixed again in 5.8.2.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top