Final edit: After reading the replies I've come to the conclusion that this was the wrong place to go to. I've had more replies about the politics and manners of the monks than an actual discussion about code. If someone responds to this with an explanation or a solution then I hope that this thread will come up in a google search for any beginners that have the same problem I do and serve as a warning. Hello, I've been trying to pass a Chinese character to a JSON hash but it always comes out as "女"

#!/usr/bin/perl use JSON; #variable declaration my $gender = "Female" #turning english selection to Chinese character if ($gender eq 'Female') { $gender = "&#22899;"; } elsif ($gender eq 'Male') { $gender = "&#30007;"; } elsif ($gender eq 'Decline to state') { $gender = ""; } my $hash_ref = {}; $hash_ref->{'detail_sex'} = $gender; print JSON->new->utf8(1)->pretty(1)->encode($hash_ref);<\code> <p>This is the result I get: { "detail_sex" : "女" } However, when I test another script it comes out perfectly.</p> <code>#!/usr/bin/perl use Digest::MD5 qw(md5 md5_hex md5_base64); use Encode qw(encode_utf8); use JSON; my $userid = 1616589; my $time = 2015811; my $ejob_id = 1908063; # md5 encryption without chinese characters my $md5_hex_sign = md5_hex($userid,$time,$job_id); print "$md5_hex_sign\n"; # seeing if character will print print "let's try encoding and decrypting \n"; print "the character to encrypt.\n"; print "&#22899;\n"; print "unicode print out\n"; print "\x{5973}\n"; my $char = "\x{5973}"; my $sign_char = "&#22899;"; print "unicode stored in \$char variable \n"; print $char, "\n"; print "md5 encryption of said chinese character from \$char with utf8 +encoding\n"; print md5_hex(encode_utf8($char)), "\n"; print "md5 encryption of wide character with utf8 encoding\n"; print md5_hex(encode_utf8("&#22899;")), "\n"; my $sign_gender = md5_hex(encode_utf8($sign_char)); #JSON print "JSON print out\n"; my $hash_ref = {}; $hash_ref->{'gender'} = $char; $hash_ref->{'md5_gender'} = md5_hex(encode_utf8($char)); $hash_ref->{'char_gender'} = md5_hex(encode_utf8("&#22899;")); $hash_ref->{'sign_gender'} = $sign_gender; print JSON->new->utf8(1)->pretty(1)->encode($hash_ref);

Here is the result: 160a6f4bf9aec1c2d102330716ca8f4e let's try encoding and decrypting the character to encrypt. 女 unicode print out Wide character in print at md5check.pl line 18. 女 unicode stored in $char variable Wide character in print at md5check.pl line 22. 女 md5 encryption of said chinese character from $char with utf8 encoding 87c835a6b1749374a7524a596087b296 md5 encryption of wide character with utf8 encoding 06c82a10da7e297180d696ed92f524c1 JSON print out { "char_gender" : "06c82a10da7e297180d696ed92f524c1", "md5_gender" : "87c835a6b1749374a7524a596087b296", "sign_gender" : "06c82a10da7e297180d696ed92f524c1", "gender" : "女" } Would someone kindly explain to me what is going on?


In reply to Trouble with Chinese characters by dweston

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.