Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

sring encode problem (hexadecimal)

by courierb (Novice)
on Jan 19, 2011 at 10:27 UTC ( #883082=perlquestion: print w/ replies, xml ) Need Help??
courierb has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'd really appreciate some help here.
for example i have a character "" (i dont know if the system will filter it out or not)
after encoded it it shows as "€"; know as hexadecimal
here is the javascripts encode and decode code which could conver the character "" to hexadecimal correctly
just as a reference i copy it below
function encode(str){ return str.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0). +replace(/(%u)(\w{4})/gi,"&#x$2;")}); } function decode(str){ return str.replace(/&#x/g,'%u').replace(/;/g,''); }
actally i want to know the equevelent of module or code perl which do the same task .
i dig into this forum found a few discussion about it. here is the one related to my question.
http://www.perlmonks.org/?node_id=654948
it seems HTML::Entities is related to my question.
I believe its decode is function well for me. i have tested it
now i try to encode it from a "double bit character" to a string in the foramt of &#xnnn( i just know its name called hexadecimal)
i have tried HTML::Entities but i cant get the expecting result from it.
here is my code. could anyone help me a bit?????
How comes it is functional well in decoding while failed in encoding??
I suspect i missed some thing but i cant find it out. please be note
i wish it's out put be "& #x20ac;" if i key in "" as the input.
<html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <title>tf8 Example</title> </head> <body> <form method="post" action="7bit.cgi"> <p>word: <input name="word" type="text"> <input type="submit" name="Submit" value=" submit "> </form> </body> </html> -------7bit.cgi--- #!/usr/bin/perl -w use CGI; use HTML::Entities; use utf8; $query = new CGI; $secretword = $query->param('word'); $remotehost = $query->remote_host(); my $a = $secretword; $a = decode_entities($a); #print encode_entities($a)."\n"; print $query->header( -charset=>'utf-8' ); print "$a";
Thanks in advance Cliff

Comment on sring encode problem (hexadecimal)
Select or Download Code
Re: sring encode problem (hexadecimal)
by JavaFan (Canon) on Jan 19, 2011 at 10:49 UTC
    I think you want (untested):
    s/([^\x00-\xFF])/sprintf "&#x%x;", $1/eg; # Encode s/&#x(\p{AHex}+);/chr hex $1/eg; # Decode
      Hi JavaFan


      It seems it is non working. i wil return a blank (nothing return)
      #!/usr/bin/perl $string=""; $string = s/([^\x00-\xFF])/sprintf "&#x%x;", $1/eg; # Encode print $string;


      Anyway thanks for the reply Have a nice day







        Use a bind operator (=~) instead of an assignment operator (=).
Re: sring encode problem (hexadecimal)
by happy.barney (Pilgrim) on Jan 19, 2011 at 10:56 UTC
    search HTML::Entities docs, it looks to me that you are searching for this method:

    encode_entities_numeric( $string, $unsafe_chars )
      Hi happy.barney Thanks for the reply. Actaully i have test it in that way
      but i am unluck to be returned an error
      i cant figure out what happened
      according to the doc i need to use
      encode_entities( $string, $unsafe_chars )


      #!/usr/bin/perl use HTML::Entities; $string=""; $encoded = encode_entities_numeric($string, '<>&"'); print $encoded;

      The error was read as :
      Undefined subroutine &main::encode_entities_numeric called at 2.cgi line 5.
      i was losted. i did the test in winxp.




      Cheers


        From the documentation of HTML::Entities:
        encode_entities_numeric( $string ) encode_entities_numeric( $string, $unsafe_chars ) This routine works just like encode_entities, except that t +he replacement entities are always "&#xhexnum;" and never "&en +tname;". For example, "encode_entities("r\xF4le")" returns "r&ocirc; +le", but "encode_entities_numeric("r\xF4le")" returns "r&#xF4;le". This routine is not exported by default. But you can alway +s export it with "use HTML::Entities qw(encode_entities_numeric);" o +r even "use HTML::Entities qw(:DEFAULT encode_entities_numeric);"

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://883082]
Approved by ELISHEVA
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-09-17 02:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (56 votes), past polls