Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Perl Monk, Perl Meditation
 
PerlMonks  

how to print 'normal' string from hex string?

by llagos (Initiate)
on Nov 22, 2011 at 16:31 UTC ( #939489=perlquestion: print w/ replies, xml ) Need Help??
llagos has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I've got a string which is in hex format. Like this:
$hex_string = "4d414a523e576573742d622053544d2d31204d532d41495328434c4 +5415229"
In 'normal' text this is "MAJR>West-b STM-1 MS-AIS(CLEAR)" How do I easily print this? I tried print hex $hex_string, but this converts to a number, so I get an Integer Overflow :( Regards, Leo

Comment on how to print 'normal' string from hex string?
Select or Download Code
Re: how to print 'normal' string from hex string?
by Eliya (Vicar) on Nov 22, 2011 at 16:37 UTC
Re: how to print 'normal' string from hex string?
by johngg (Abbot) on Nov 22, 2011 at 17:36 UTC

    Expanding Eliya's reply slightly.

    knoppix@Microknoppix:~$ perl -E ' > $hex = q{4d414a523e576573742d622053544d2d31204d532d41495328434c45415 +229}; > $str = unpack q{a*}, pack q{H*}, $hex; > say $str;' MAJR>West-b STM-1 MS-AIS(CLEAR) knoppix@Microknoppix:~$

    As BrowserUk points out, the unpack here is not necessary.

    Cheers,

    JohnGG

      What is the unpack 'a*' bit for?

      print pack 'H*', "4d414a523e576573742d622053544d2d31204d532d4149532843 +4c45415229";; MAJR>West-b STM-1 MS-AIS(CLEAR)

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        What is the unpack 'a*' bit for?

        It is a side effect of my still somewhat flaky understanding of pack/unpack or, in other words, completely un-necessary :-(

        I will update my post.

        Cheers,

        JohnGG

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (11)
As of 2014-04-16 10:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (424 votes), past polls