Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Converting ASCII to Hex

by merlyn (Sage)
on Jul 21, 2005 at 01:01 UTC ( [id://476679]=note: print w/replies, xml ) Need Help??


in reply to Converting ASCII to Hex

my $hex_string = unpack "H*", $ascii_string;

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re^2: Converting ASCII to Hex
by freonpsandoz (Beadle) on Jun 11, 2017 at 00:25 UTC

    Any chance of updating this topic now that unpack doesn't work as stated on strings in post-5.1 versions? I don't really want to have to "use bytes". Data::Dumper doesn't give me what I want: it shows the string contents with Unicode characters turned into something like "\x{2013}" (whatever that is) instead of the expected "E2 80 93". Thanks.

      Here's one method. Doubtless there's a neater approach but this works for your single example. Feel free to add to @tests to convince yourself that it is doing what you want.

      #!/usr/bin/env perl use strict; use warnings; use Encode; use Test::More; my @tests = ( { source => "\x{2013}", want => 'E2 80 93' } ); plan tests => scalar @tests; for my $t (@tests) { Encode::_utf8_off ($t->{source}); # bytes my $have = uc unpack "H*", $t->{source}; # into hex $have = join (' ', $have =~ /(..)/g); # spaced pairs is ($have, $t->{want}, "Covert $t->{source} into $t->{want}"); }
Re^2: Converting ASCII to Hex
by Anonymous Monk on Jul 21, 2005 at 01:08 UTC
    That did not seem to work My Hex codes are coming out like "313030" instead of things like "A5" or "0C" etc...
      If the input string is made of digits, you will see an hex like that you're describing. Consider that in ASCII the digits characters range from decimal 48, which is 0x30, to decimal 57, which is 0x39.

      If you have numbers that you want to treat like integers, and they're in the range 0-255, you can put a chr in the chain:

      #!/usr/bin/perl use strict; use warnings; my @values = (0, 1, 5, 25, 100, 200, 255); print join(", ", map { unpack "H*", chr } @values), $/; __END__ 00, 01, 05, 19, 64, c8, ff
      You may also consider printf/sprintf with the X indicator:
      #!/usr/bin/perl use strict; use warnings; my @values = (0, 1, 5, 25, 100, 200, 255, 500, 1000); print join(", ", map { sprintf "%X", $_ } @values), $/; __END__ 0, 1, 5, 19, 64, C8, FF, 1F4, 3E8
      Note that this second solution works well with numbers beyond 255. You can also pad with zeroes on the left, just look in the docs for sprintf.

      Flavio
      perl -ple'$_=reverse' <<<ti.xittelop@oivalf

      Don't fool yourself.
      Well, 313030 are the hex equivelents for the three characters "1", "0" and "0". Without knowing what your data is, we don't know if that's right or wrong. To clarify matters, are you sure you want the ASCII values of the characters, or do you want the hex representation of the number 100?

      You are missing what merlyn is saying. Use the unpack on the original string, not on the array of ordinals.

      If you run it on the ordinals, then you are converting strings that are made up of digits, so you will get back strings in the range 30..39.

      Update: The following should be more than sufficient to replace the logic in the OP:

      use strict; use warnings; open my $in, "<", "./infile"; my $input = do { local $/; <$in> }; open my $out, ">", "./outfile"; print $out unpack 'H*', $input;

      Though presumably you aren't hardcoding filenames in real life.

      Was your test string "100" by chance? :) 31 = hex 1, 30 = hex 0. You're getting the right output, it just doesn't look like what you expected.

      Here's an example you can try:
      my $text = 'WHY HELLO THERE!'; # split into array of individual characters my @characters = split(//,$text); # transform each one individually into uppercase hex foreach my $char (@characters) { $char = uc(unpack "H*", $char); } # print in quotes to demonstrate separation between array elements print "@characters";
      You can cross-check the results with http://www.lookuptables.com.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-25 10:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found