Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hello fellow Monks,

I am trying to convert an Chinese string to hexadecimal (final form) and vise versa, but I wanted also to see the in between steps.

I tried successfully to convert the string to utf-8 and then I was thinking of converting it into ascii characters and then to hex with the help of String::HexConvert. If that would work I would revert the process to the original form.

I manage to accomplish my task for utf-8 and UCS-2 encoding. I though of sharing the script, since I was not able to find further information online.

Similar question (How do I convert a sequence of hexes (D0 D6) to Chinese characters (中)?). I used the following modules (Text::Unidecode) and also (String::HexConvert).

Sample of code:

#!/usr/bin/perl
use utf8;
use strict;
use warnings;
use feature 'say';
use Text::Unidecode;
use Encode qw(decode encode);
use String::HexConvert ':all';

binmode( STDOUT, ':utf8' );
my $Chinese = '北亰'; # Chinese characters for Bei Jing (U+5317 U+4EB0)

say 'UTF-8';
my $utf8 = encode( 'UTF-8', $Chinese );

my $ascii2hexUTF8 = ascii_to_hex($utf8);
say join(' ', split(/(..)/, $ascii2hexUTF8));

my $hex2ascciiUTF8 = hex_to_ascii($ascii2hexUTF8);

my $strUTF8 = decode( 'UTF-8', $hex2ascciiUTF8);
say $strUTF8;
say unidecode( $strUTF8 );

say '';

say 'UCS-2';
my $ucs2 = encode("UCS-2BE", $Chinese);

my $ascii2hexUCS2 = ascii_to_hex($ucs2);
say join(' ', split(/(..)/, $ascii2hexUCS2));

my $hex2ascciiUCS2 = hex_to_ascii($ascii2hexUCS2);

my $strUCS2 = decode("UCS-2BE", $hex2ascciiUCS2);
say $strUCS2;
say unidecode( $strUCS2 );

__END__

$ perl chinese.pl
UTF-8
 e5  8c  97  e4  ba  b0
北亰
Bei Jing

UCS-2
 53  17  4e  b0
北亰
Bei Jing

Hope this helps someone else as it did to me, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Chinese to Hex and Hex to Chinese by thanos1983

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (2)
    As of 2017-12-17 08:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      What programming language do you hate the most?




















      Results (462 votes). Check out past polls.

      Notices?