1.
byte[] bytes = "test.message".getBytes("UTF-8"); //result: [116, 101, 115, 116, 46, 109, 101, 115, 115, 97, 103, 101]
2.
JavaScript has no concept of character encoding for String, everything is in UTF-16.
Most of time time the value of a char
in UTF-16 matches UTF-8, so you can forget it's any different.
There are more optimal ways to do this but
function s(x) {return x.charCodeAt(0);} "test.message".split('').map(s); // [116, 101, 115, 116, 46, 109, 101, 115, 115, 97, 103, 101]
3.
// encode(decode) html text into html entity var decodeHtmlEntity = function(str) { return str.replace(/&#(\d+);/g, function(match, dec) { return String.fromCharCode(dec); }); }; var encodeHtmlEntity = function(str) { var buf = []; for (var i=str.length-1;i>=0;i--) { buf.unshift(['&#', str[i].charCodeAt(), ';'].join('')); } return buf.join(''); }; var entity = '高级程序设计'; var str = '高級程序設計'; console.log(decodeHtmlEntity(entity) === str); console.log(encodeHtmlEntity(str) === entity); // true // true
4. transform text in utf8 format to string
escape()和unescape()是一對編碼解碼函數,一般用於URL中非ASCII字符的編碼和解碼
如:escape('&')返回%26
unescape('%26')返回&,都用十六進制編碼
var s = '\u7cfb\u7edf\u9519\u8bef';
unescape(s) //系統錯誤
參考: