unicode string

V

volantecho

Hi,

Code :
$string = "0041";
$hexcode = '\x{'.$string.'}';
$hexcode2 = "\x{0041}";

use Data::Hexdumper;
$hexcode = hexdump(data=>$hexcode);

print $hexcode; # show \x{0041}
print $hexcode2; # show A

What can i do to make $hexcode shown as $hexcode2?
Thanks for every comments
 
A

Anno Siegel

Hi,

Code :
$string = "0041";
$hexcode = '\x{'.$string.'}';
$hexcode2 = "\x{0041}";

use Data::Hexdumper;
$hexcode = hexdump(data=>$hexcode);

print $hexcode; # show \x{0041}
print $hexcode2; # show A

What can i do to make $hexcode shown as $hexcode2?

I'm not sure what "make $hexcode shown as ..." means. You can let Perl
evaluate the string in $hexcode as a literal:

print eval "qq($hexcode)", "\n";

Anno
 
B

Brian McCauley

Anno said:
I'm not sure what "make $hexcode shown as ..." means. You can let Perl
evaluate the string in $hexcode as a literal:

print eval "qq($hexcode)", "\n";

Be aware that if the OP's intent is to allow \x{} escapes in data files
then using eval allows interpolation with $ and @ and also allow the
execution of arbitrary malicious code.
 
D

Donald King

Hi,

Code :
$string = "0041";
$hexcode = '\x{'.$string.'}';
$hexcode2 = "\x{0041}";

use Data::Hexdumper;
$hexcode = hexdump(data=>$hexcode);

print $hexcode; # show \x{0041}
print $hexcode2; # show A

What can i do to make $hexcode shown as $hexcode2?
Thanks for every comments

There have been some other suggestions, but yet another way to do it:

$hexcode =~ s/\\x\{([0-9A-Fa-f]+)\}/chr(hex($1))/eg;

That'd be a good way if, for instance, you're reading in all these
\x{}'s from a file or something.

However, if you just want to create arbitrary Unicode characters in your
own code, just use chr.

$hexcode = chr(0x0041);
 
D

Dr.Ruud

Donald King schreef:
$hexcode =~ s/\\x\{([0-9A-Fa-f]+)\}/chr(hex($1))/eg;

Or make use of [[:xdigit:]] (see perlre).

Expect to see something similar to "\x{06EE 002E 06EE}" in Perl6.

However, if you just want to create arbitrary Unicode characters in
your own code, just use chr.

$hexcode = chr(0x0041);

Unless there is more than one involved: "\x{6EE}\x{2E}\x{6EE}".



After the parser has done its work, I expect that the constants 'A',
chr(0x0041) and "\x{41}" are unified. Let's try:

$ echo '$a = q(A); $b = chr(0x41); $c = "\x{41}"'
| perl -MO=Deparse,-x7

$a = 'A';
$b = 'A';
$c = 'A';



And:

$ echo '$a = q(A); $b = chr(0x06EE); $c = "\x{06EE}"'
| perl -MO=Deparse,-x7

$a = 'A';
$b = "\x{6ee}";
$c = "\x{6ee}";


It seems that Deparse finds a string literal "more constant" than a
constant expression involving a chr().
 
J

jl_post

cool! i use eval then solve the problem!!
thanks a lot for you guys help :)


Er... I wouldn't recommend using a string eval() to do this.
Certain posters (like Uri Guttman and A. Sinar Unur) advise against
using them, and for the most part I happen to agree with them.

Consider using this instead:

my $string = "0041";
my $letter = chr( hex($string) ); # $letter is now set to 'A'

Explanation: The hex() function converts a hex string (in our
example, "0041") into a numerical value (65). That value is then
passed into the chr() function which returns character 'A' (ASCII
character 65) which is exactly what you asked for.

If you want to read more about the hex() and chr() functions,
they're just a few keystrokes away. Just type the following at your
command prompt:

perldoc -f hex
perldoc -f chr

I hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top