Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

String to hex

by jasal (Initiate)
on Aug 09, 2012 at 13:18 UTC ( #986516=perlquestion: print w/ replies, xml ) Need Help??
jasal has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I would need to get for example this:
COM7:
to look like this:
0x4F43
0x374D
0x003A
0x0000

I know that I can do this:
$string = "COM7:"; $string =~ s/(.)/sprintf("%02X",ord($1))/seg; print "$string\n";
To get this: 434F4D373A, which is sort of correct, but what would be most convinient way to arrange it to look like what I need, and print it to a file?

Comment on String to hex
Download Code
Replies are listed 'Best First'.
Re: String to hex
by Anonymous Monk on Aug 09, 2012 at 13:34 UTC
    $ perl -le " printf qq{0x%04s\n},$_ for unpack '((H4)3)(H)', qq/COM7: +/ " 0x434f 0x4d37 0x003a 0x0000
      $ perl -le " printf qq{0x%04s\n},$_ for unpack '((H4)*)(H)', qq/COM7: +/ " 0x434f 0x4d37 0x003a 0x0000
Re: String to hex
by thundergnat (Deacon) on Aug 09, 2012 at 14:50 UTC
    $string = "COM7:"; $string =~ s/(.)(.)?/sprintf("0x%02X%02X\n",ord($2),ord($1))/seg; print "${string}0x0000\n";
    0x4F43
    0x374D
    0x003A
    0x0000
    
Re: String to hex
by trizen (Hermit) on Aug 09, 2012 at 14:20 UTC
    use strict; use warnings; my $text = "COM7:"; my @pairs = unpack "(A2)*", uc unpack "H*", $text; for (my $i = 0 ; $i <= $#pairs ; ++$i) { printf "0x%02s%02s\n", reverse $pairs[$i] || 0, $pairs[++$i] || 0; } print "0x0000\n"; # :-) __END__ 0x4F43 0x374D 0x003A 0x0000
Re: String to hex
by influx (Beadle) on Aug 09, 2012 at 13:45 UTC

    This seems pretty legible

    sub str_to_hex { my $str = shift; return "0x" . substr(unpack("H*", $str), 0, 4); } print str_to_hex("COM7") . "\n"; # outputs: 0x434f
      It needs to produce 4 lines, not just one

        Eh. Really? OK, well, I would advise against this method, but I was determined to get it working using my original unpack version.. so here goes

        sub str_to_hex { my $str = shift; my @h; for my $s (split(/(....)/, unpack("H*", $str))) { if ($s ne '') { if (length($s) < 4) { $s = ("0" x (4 - int(length($s)))) . $s; } push @h, "0x" . $s; } } push @h, "0x0000" while @h < 4; return @h; } for (str_to_hex("COM7:")) { print $_ . "\n"; }

        This outputs
        0x434f
        0x4d37
        0x003a
        0x0000

Re: String to hex
by johngg (Abbot) on Aug 10, 2012 at 23:34 UTC

    Well behind the fair on this one but better late than never :-/

    $ perl -E ' > printf qq{0x%04X\n}, $_ for unpack q{n*}, join q{}, > reverse unpack q{(a2)4}, reverse pack q{a8}, q{COM7:};' 0x4F43 0x374D 0x003A 0x0000 $

    Cheers,

    JohnGG

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2015-07-31 06:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls