hex print

Discussion in 'Perl Misc' started by Trifle Menot, Feb 25, 2014.

  1. Trifle Menot

    Trifle Menot Guest

    To print a string
    in hex, separated by single spaces
    can it be done without looping?
     
    Trifle Menot, Feb 25, 2014
    #1
    1. Advertisements

  2. [rw@sable]~#perl -e 'print(join(" ", unpack("(H2)*", "abc123")), "\n");'
    61 62 63 31 32 33
     
    Rainer Weikusat, Feb 25, 2014
    #2
    1. Advertisements

  3. Trifle Menot

    Kaz Kylheku Guest

    ... and for bonus points, while chained and straitjacketed, suspended in a
    plexiglass tank full of water, and without using the character $ anywhere?
     
    Kaz Kylheku, Feb 25, 2014
    #3
  4. Trifle Menot

    Trifle Menot Guest

    Thanks.

    But to minimize it, I think I will eliminate the print() and unpack()
    parentheses.

    fw:~/temp # perl -e 'print join (" ", unpack "(H2)*", "abc123"), "\n";'
    61 62 63 31 32 33

    The (H2) parentheses create list context, I suppose? That was the
    tricky part I couldn't work out on my own.
     
    Trifle Menot, Feb 25, 2014
    #4
  5. Trifle Menot

    Trifle Menot Guest

    I do have a need for it, and wanted to find the best way.
     
    Trifle Menot, Feb 25, 2014
    #5
  6. Στις 25/2/2014 21:48, ο/η Trifle Menot έγÏαψε:
    $_ = 'abc123';
    s/\w/unpack('H2',$&).' '/ge;
    print;
     
    George Mpouras, Feb 25, 2014
    #6
  7. It make no sense to talk about 'list context' here as 'pack' and
    'unpack' specifiers are written in a formal language other than
    perl. The () mark a group here so (for unpack)

    (H2)*

    means 'as many two-digit hex numbers as can be pulled out of the
    input'.
     
    Rainer Weikusat, Feb 25, 2014
    #7
  8. Trifle Menot

    Trifle Menot Guest

    I thought an asterisk, by itself, meant "as many as can be pulled from
    the input." That was one of the things I tried earlier, and failed. But
    I threw that code away, so I don't remember exactly what I tried.

    The parentheses seem to add some magic. I thought the "magic" was list
    context. But I'll take your word for it.
     
    Trifle Menot, Feb 25, 2014
    #8
  9. T> To print a string
    T> in hex, separated by single spaces

    T> can it be done without looping?

    No. Something will need to iterate; the best you can accomplish is
    making it implicit rather than explicit.

    Charlton
     
    Charlton Wilbur, Feb 25, 2014
    #9
  10. Trifle Menot

    John Bokma Guest

    Don't: perldoc -f pack

    "
    · A ()-group is a sub-TEMPLATE enclosed in parentheses.
    A group may take a repeat count, both as postfix,
    "
     
    John Bokma, Feb 25, 2014
    #10
  11. Trifle Menot

    Trifle Menot Guest

    I know what assembly language is, so I understand what you mean. But
    those words may confuse people who only want to learn enough Perl to
    solve their immediate problem.

    It's not always wise to say everything you know.
     
    Trifle Menot, Feb 25, 2014
    #11
  12. Is this supposed to contradict


    ,----
    |The () mark a group here so (for unpack)
    |
    |(H2)*
    |
    |means 'as many two-digit hex numbers as can be pulled out of the
    |input'.
    `----#

    ?

    If so, how?
     
    Rainer Weikusat, Feb 25, 2014
    #12
  13. Trifle Menot

    John Bokma Guest

    No, it's not a contradiction. But a recommendation not to take someone's
    word on Usenet for something if one can read the documentation.

    Don't take it personal, it was not meant that way, and my apologies if
    you read it as such.
     
    John Bokma, Feb 25, 2014
    #13
  14. Trifle Menot

    Trifle Menot Guest

    I read that earlier, when I was trying and failing.

    But with so much scalar and list context swirling around in my head, it
    just didn't sink in the first time.

    Seems clear now though.
     
    Trifle Menot, Feb 25, 2014
    #14
  15. Trifle Menot

    Trifle Menot Guest

    Right. I did not take it as a contradiction.

    And I agree it's best to read first and ask questions later. But some of
    us are slow, and read something 10 times before we get it.

    Slow doesn't mean stupid, it just means slow. People learn in different
    ways and at different rates. A little help from another human can speed
    things along.
     
    Trifle Menot, Feb 25, 2014
    #15
  16. Trifle Menot

    Dr.Ruud Guest

    It can also not be done without electricity.
    TIMTOWDI: there is (always) more than one (decent) way to do it.

    A better general question is:
    What practical{, joker} ways are there to ...


    Driest is probably:

    perl -wE'
    my $data = "abc123";
    say join " ", unpack "(H2)*", $data;
    '
    61 62 63 31 32 33
     
    Dr.Ruud, Feb 26, 2014
    #16
  17. Trifle Menot

    Trifle Menot Guest

    In this case maybe not.

    I consider print vs. say immaterial. The essence of the solution is:

    a) unpack a string to a list of hex values
    b) rejoin it with spaces

    Both steps in one line of code. I don't see how that could be improved.
     
    Trifle Menot, Feb 26, 2014
    #17
  18. T> On Tue, 25 Feb 2014 16:29:00 -0500, Charlton Wilbur

    T> I know what assembly language is, so I understand what you
    T> mean. But those words may confuse people who only want to learn
    T> enough Perl to solve their immediate problem.

    A little learning is a dangerous thing;
    drink deep, or taste not the Pierian spring.

    T> It's not always wise to say everything you know.

    And it is inordinately foolish to ask idiotic questions and expect to
    not have the idiocy pointed out.

    Charlton
     
    Charlton Wilbur, Feb 26, 2014
    #18
  19. Trifle Menot

    Trifle Menot Guest

    Thanks for the pseudo intellectual advice, Charlton.
     
    Trifle Menot, Feb 26, 2014
    #19
  20. The question wasn't idiotic. It was somewhat academic (Solve problem A
    without using obvious feature X), but that's often a good way to learn,
    and especially in an interpreted language it is often worthwhile to use
    complex builtins instead of explicitely coding a loop (less interpreter
    overhead).

    It is - well, I'd like to avoid words like "foolish" or "idiotic" here -
    not useful to give an answer which is obviously true at some level, but
    doesn't match the level of the question. Yes, using perl to run Perl
    programs always involves loops. The compiler will loop over the source
    text building the byte code[1], the interpreter will loop over the byte
    code. Many primitive operations will also involve loops. @a = @b will
    loop over both arrays. m/foo.*bar/ will loop over $_ and the pattern.
    But at the level of the Perl language these are primitive operations,
    not loops.

    hp

    [1] Is there a more generic term for this? It's not *byte* code after
    all.
     
    Peter J. Holzer, Feb 27, 2014
    #20
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.