Я пытаюсь преобразовать строку unicode в шестнадцатеричное представление в javascript.
Это то, что у меня есть:
function convertFromHex(hex) {
var hex = hex.toString();//force conversion
var str = '';
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}
function convertToHex(str) {
var hex = '';
for(var i=0;i<str.length;i++) {
hex += ''+str.charCodeAt(i).toString(16);
}
return hex;
}
Но если сбой в символах юникода, например, китайский;
Input: 漢字
Вывод: ªo "[Вт
Любые идеи? Это можно сделать в javascript?