Perl golf: printing data in hex

Discussion in 'Perl Misc' started by A. Farber, Mar 3, 2004.

  1. A. Farber

    A. Farber Guest

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

  2. A. Farber

    Thomas Kratz Guest

    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^..
     
    Thomas Kratz, Mar 3, 2004
    #2
    1. Advertising

  3. A. Farber

    Ron Parker Guest

    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}}
     
    Ron Parker, Mar 3, 2004
    #3
  4. A. Farber

    fifo Guest

    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
     
    fifo, Mar 3, 2004
    #4
  5. A. Farber

    Jay Tilton Guest

    (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.
     
    Jay Tilton, Mar 3, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,388
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    525
    Juha Autero
    Aug 19, 2003
  3. Martin DeMello

    GOLF: hex string to dotted quad

    Martin DeMello, Dec 6, 2006, in forum: Ruby
    Replies:
    9
    Views:
    144
    Max Muermann
    Dec 6, 2006
  4. Alec Taylor

    [Perl Golf] Round 1

    Alec Taylor, Feb 5, 2012, in forum: Python
    Replies:
    0
    Views:
    151
    Alec Taylor
    Feb 5, 2012
  5. Scott

    perl golf techniques

    Scott, Dec 18, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    268
    Jay Tilton
    Dec 19, 2003
Loading...

Share This Page