unicode string

Discussion in 'Perl Misc' started by volantecho@gmail.com, Mar 1, 2006.

  1. Guest

    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
    , Mar 1, 2006
    #1
    1. Advertising

  2. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > 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
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Mar 1, 2006
    #2
    1. Advertising

  3. Anno Siegel wrote:
    > <> wrote in comp.lang.perl.misc:
    > > 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";


    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.
    Brian McCauley, Mar 1, 2006
    #3
  4. Guest

    cool! i use eval then solve the problem!!
    thanks a lot for you guys help :)
    , Mar 1, 2006
    #4
  5. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > cool! i use eval then solve the problem!!
    > thanks a lot for you guys help :)


    You have seen Sinan's warning, haven't you?

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Mar 1, 2006
    #5
  6. Donald King Guest

    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/
    Donald King, Mar 1, 2006
    #6
  7. Dr.Ruud Guest

    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."
    Dr.Ruud, Mar 1, 2006
    #7
  8. Guest

    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.
    , Mar 1, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Mark Bram
    Replies:
    0
    Views:
    3,902
    Robert Mark Bram
    Sep 28, 2003
  2. ygao

    unicode wrap unicode object?

    ygao, Apr 8, 2006, in forum: Python
    Replies:
    6
    Views:
    525
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Apr 8, 2006
  3. Gabriele *darkbard* Farina

    Unicode digit to unicode string

    Gabriele *darkbard* Farina, May 16, 2006, in forum: Python
    Replies:
    2
    Views:
    493
    Gabriele *darkbard* Farina
    May 16, 2006
  4. Holger Joukl
    Replies:
    5
    Views:
    512
    Ben Finney
    Dec 13, 2006
  5. Chirag Mistry
    Replies:
    6
    Views:
    159
    Ollivier Robert
    Feb 8, 2008
Loading...

Share This Page