Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl golf: printing data in hex

Reply
Thread Tools

Perl golf: printing data in hex

 
 
A. Farber
Guest
Posts: n/a
 
      03-03-2004
Given a binary string in $x, what would be the shortest way
to print it out in hex, like: 3A BC DE FF 00 .. .. .. ?
 
Reply With Quote
 
 
 
 
Thomas Kratz
Guest
Posts: n/a
 
      03-03-2004
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 .. .. .. ?


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

Thomas

--
open STDIN,"<&DATA";$=+=14;$%=50;while($_=(seek( #J~.> a>n~>>e~.......>r.
STDIN,$:*$=+$,+$%,0),getc)){/\./&&last;/\w| /&&( #.u.t.^..oP..r.>h>a~.e..
print,$_=$~);/~/&&++$:;/\^/&&--$:;/>/&&++$,;/</ #.>s^~h<t< ..~. ...c.^..
&&--$,;$:%=4;$,%=23;$~=$_;++$i==1?++$,:_;}__END__#.... >>e>r^..>l^...>k^..
 
Reply With Quote
 
 
 
 
Ron Parker
Guest
Posts: n/a
 
      03-03-2004
On 3 Mar 2004 09:06:11 -0800, 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 .. .. .. ?


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

--
#macro R(L P)sphere{L F}cylinder{L P F}#end#macro P(V)merge{R(z+a z)R(-z a-z)R(a
-z-z-z a+z)torus{1F clipped_by{plane{a 0}}}translate V}#end#macro Z(a F T)merge{
P(z+a)P(z-a)R(-z-z-x a)pigment{rgbf 1}hollow interior{media{emission 3-T}}}#end
Z(-x-x.2x)camera{location z*-10rotate x*90normal{bumps.02scale.05}}
 
Reply With Quote
 
fifo
Guest
Posts: n/a
 
      03-03-2004
At 2004-03-03 09:06 -0800, 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 .. .. .. ?


print uc join' ',unpack'(H2)*',$x
 
Reply With Quote
 
Jay Tilton
Guest
Posts: n/a
 
      03-03-2004
http://www.velocityreviews.com/forums/(E-Mail 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hex string to hex value tim Python 8 11-23-2005 06:27 PM
[Fwd: Re: hex string to hex value] tim Python 2 11-23-2005 07:18 AM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
hex value in string back to real hex value jack Python 4 09-08-2004 07:11 AM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments