http://www.perlmonks.org?node_id=1196219
thanos1983 has asked for the wisdom of the Perl Monks concerning the following question:

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!