Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re^2: Converting ASCII to Hex

by freonpsandoz (Beadle)
on Jun 11, 2017 at 00:25 UTC ( #1192498=note: print w/replies, xml ) Need Help??

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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2022-08-18 01:22 GMT
Find Nodes?
    Voting Booth?

    No recent polls found