Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Howto convert hex to string

by bh_perl (Monk)
on Jun 27, 2003 at 08:49 UTC ( [id://269476]=perlquestion: print w/replies, xml ) Need Help??

bh_perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm very new in perl programming, anybody can guide me how to convert hex to string, as example : 84 67 49 67 83 50 79 ====> TC1CS2O

Replies are listed 'Best First'.
Re: Howto convert hex to string
by Tomte (Priest) on Jun 27, 2003 at 08:59 UTC

    This data isn't hex, but decimal (according to man ascii anyway)

    $> perl -e 'print pack("i*", $_) for(qw/84 67 49 67 83 50 79/)' TC1CS2O

    If you have really hex-values, change i to h

    perl -e 'print pack("h*", $_) for(qw/H E X V A L U E S/)'
    for a closer look, read perldoc -f pack.

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

      Or simpler still
      shell> perl -le 'print map chr, qw/84 67 49 67 83 50 79/' TC1CS2O
      And if you're dealing with hex values
      shell> perl -le 'print map { chr hex } qw/54 43 31 43 53 32 4f/' TC1CS2O
      See. map, chr and hex for more info.
      HTH

      _________
      broquaint

Re: Howto convert hex to string
by jmcnamara (Monsignor) on Jun 27, 2003 at 09:07 UTC

    In the example that you have shown the characters are in decimal and not hex. To convert them use chr:
    #!/usr/bin/perl -wl my @nums = qw(84 67 49 67 83 50 79); my @chars = map {chr} @nums; print join '', @chars; __END__ Prints: TC1CS2O

    If the numbers were in hex you could convert them with hex:

    my @chars = map {chr hex} @nums;

    --
    John.

Re: Howto convert hex to string
by BrowserUk (Patriarch) on Jun 27, 2003 at 10:38 UTC

    Easier still? Use 'C*' with pack.

    perl58 -e" print pack 'C*', qw[84 67 49 67 83 50 79] "; TC1CS2O

    Not that it matters, but its a sight quicker than map{ chr } @chars; :)


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Re: Howto convert hex to string (and converting to binary)
by crabbdean (Pilgrim) on Jul 18, 2004 at 11:06 UTC
    This post helped me with something. So giving back graciously (as an adjunct to this topic) can also convert to binary like this
    #!perl use strict; use warnings; map { print unpack "B*", chr hex } qw\25 00 53 00 79 00 73 00 74 00 65 + 00 6d 00 52 00 6f 00 6f 00 74 00 25 00 5c 00 73 00 79 00 73 00 74 00 + 65 00 6d 00 33 00 32 00 5c 00 53 00 48 00 45 00 4c 00 4c 00 33 00 32 + 00 2e 00 64 00 6c 00 6c 00 00 00\;

    Dean
    The Funkster of Mirth
    Programming these days takes more than a lone avenger with a compiler. - sam
    RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-25 15:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found