java string.getBytes(“UTF-8”) javascript equivalent


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) //系統錯誤

 

參考:

Best practice: escape, or encodeURI / encodeURIComponent


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM