http://qs1969.pair.com?node_id=1192498


in reply to Re: Converting ASCII to Hex
in thread Converting ASCII to Hex

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.

Replies are listed 'Best First'.
Re^3: Converting utf-8 to Hex
by hippo (Bishop) on Jun 11, 2017 at 11:42 UTC

    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}"); }