Here is php code:
$arr=array(228,184,173,230,150,135,99,104,105,110,101,115,101);
$str='';
foreach ($arr as $i){
$str.=chr($i);
}
print $str;
the output is: 中文chinese
Here is javascript code:
var arr=[228,184,173,230,150,135,99,104,105,110,101,115,101];
var str='';
for (i in arr){
str+=String.fromCharCode(arr[i]);
}
console.log(str);
the output is: ä¸æchinese
So how should I process the array at javascript?
the result will be
['3','5','7','9']
the result will be
[3,5,7,9]
Seems the best way these days is the following:
Chinese charset has a different encoding in which one char is more than one byte long. When you do this
You are converting each byte to a char(actually string) and adding it to a string str. What you need to do is, pack the bytes together.
I changed your array to this and it worked for me:
I got these codes from here.
you can also take a look at this for packing bytes to a string.
Another solution without
decodeURIComponent
for characters up to 3 bytes (U+FFFF). The function presumes the string is valid UTF-8, not much error checking...JavaScript strings consist of UTF-16 code units, yet the numbers in your array are the bytes of a UTF-8 string. Here is one way to convert the string, which uses the decodeURIComponent() function:
Performing the UTF-8 to UTF-16 conversion in the conventional way is likely to be more efficient but would require more code.