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

Comment on

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

Hello fellow Monks,

I am trying to convert an Arabic string to hexadecimal (final form) and vise versa, but I am lost in between the 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.

Sample of code:

#!/usr/bin/perl
use utf8;
use strict;
use warnings;
use feature qw(say);
use Encode qw(decode encode);

binmode( STDOUT, ':utf8' );

my $arabic = 'ﻟﻠﺒﻴﻊ';                 # "For sale"
my $utf8 = encode( 'UTF-8', $arabic );
my $str = decode( 'UTF-8', $utf8);

say 'UTF-8';
say $arabic; # ﻟﻠﺒﻴﻊ
say $utf8;
say $str; # ﻟﻠﺒﻴﻊ
say '';

my $ucs2 = encode("UCS-2BE", $arabic);
my $decoded = decode("UCS-2BE", $ucs2);

say 'UCS-2';
say $arabic; # ﻟﻠﺒﻴﻊ
say $ucs2;
say $decoded; # ﻟﻠﺒﻴﻊ

__END__

$ perl test.pl
UTF-8
ﻟﻠﺒﻴﻊ
Ÿﻠ’ﻴŠ
ﻟﻠﺒﻴﻊ

UCS-2
ﻟﻠﺒﻴﻊ
’
ﻟﻠﺒﻴﻊ

I am getting lost on how to convert the utf-8 string to ascii, am I thinking correctly, or I am thinking completely wrong. The whole goal is to convert the Arabic string into hexadecimal and vise versa, it does not really matter for me how many conversions I have to make as long as it works. But so far none of the conversions have worked.

Anyone has any idea on how to convert utf-8 to ascii for Arabic characters, is this possible if not any ideas?

Update: Finally managed to make it work. See sample bellow:

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

binmode( STDOUT, ':utf8' );

my $arabic = 'ﻟﻠﺒﻴﻊ';                 # "For sale"
say $arabic;
say 'UTF-8';
my $utf8 = encode( 'UTF-8', $arabic );

my $ascii2hexUTF8 = ascii_to_hex($utf8);
say $ascii2hexUTF8;

my $hex2ascciiUTF8 = hex_to_ascii($ascii2hexUTF8);
say $hex2ascciiUTF8;

my $strUTF8 = decode( 'UTF-8', $hex2ascciiUTF8);
say $strUTF8;
say '';
say 'UCS-2';
my $ucs2 = encode("UCS-2BE", $arabic);

my $ascii2hexUCS2 = ascii_to_hex($ucs2);
say $ascii2hexUCS2;

my $hex2ascciiUCS2 = hex_to_ascii($ascii2hexUCS2);
say $hex2ascciiUCS2;

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

__END__

$ perl test.pl
ﻟﻠﺒﻴﻊ
UTF-8
efbb9fefbba0efba92efbbb4efbb8a
Ÿﻠ’ﻴŠ
ﻟﻠﺒﻴﻊ

UCS-2
fedffee0fe92fef4feca
’
ﻟﻠﺒﻴﻊ

Now the question is, how could I get hexadecimal output in a form like (sample):

00 31 00 2e

Update2: I added the sample of join and split expected output:

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

binmode( STDOUT, ':utf8' );

my $arabic = 'ﻟﻠﺒﻴﻊ';                 # "For sale"

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

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 '';

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

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

my $hex2ascciiUCS2 = hex_to_ascii($ascii2hexUCS2);

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

__END__

$ perl test.pl
UTF-8
 ef  bb  9f  ef  bb  a0  ef  ba  92  ef  bb  b4  ef  bb  8a
ﻟﻠﺒﻴﻊ

UCS-2
 fe  df  fe  e0  fe  92  fe  f4  fe  ca
ﻟﻠﺒﻴﻊ

I can split the hexadecimal string into two strings but is this correct? Is any other way to see the hexadecimal in other format? For example form the hex documentation they represent the hex as '0xAf' should I added manually or is there any other way to get the hex like this?

Update3: Thanks to fellow monk choroba I modified the <code> to <pre>.

Thanks everyone in advance for their time and effort.

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

In reply to Arabic to Hex and Hex to Arabic 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 rifling through the Monastery: (5)
    As of 2017-10-21 07:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My fridge is mostly full of:

















      Results (269 votes). Check out past polls.

      Notices?