0 Replies - 666 Views - Last Post: 03 September 2015 - 02:03 PM

#1 dweston  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-September 12

Trouble with Chinese characters

Posted 03 September 2015 - 02:03 PM

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 = "女";
#$gender = "\x{5973}";
		} elsif ($gender eq 'Male') {
				$gender = "男";
#$gender = "\x{7537}";
		} 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);


This is the result I get:

{
"detail_sex" : "女"
}


However, when I test another script it comes out perfectly.

#!/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 "女\n";
print "unicode print out\n";
print "\x{5973}\n";
my $char = "\x{5973}";
my $sign_char = "女";
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("女")), "\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("女"));
$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?

Is This A Good Question/Topic? 0
  • +

Page 1 of 1