|
From: Jos? Luis P?rez Diez on 1 Mar 2006 11:30 In article <1141227241.636401.90590(a)p10g2000cwp.googlegroups.com>, wrote: > cool! i use eval then solve the problem!! > thanks a lot for you guys help :) > :( don't use eval when you could use chr: http://perldoc.perl.org/functions/chr.html
From: Donald King on 1 Mar 2006 13:18 volantecho(a)gmail.com wrote: > 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); -- Donald King, a.k.a. Chronos Tachyon http://chronos-tachyon.net/
From: Dr.Ruud on 1 Mar 2006 13:56 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(). -- Affijn, Ruud "Gewoon is een tijger."
From: jl_post@hotmail.com on 1 Mar 2006 15:41
volantecho(a)gmail.com wrote: > 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. |