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 = 'u7cfbu7edfu9519u8bef';
unescape(s) //系统错误

参考:

Best practice: escape, or encodeURI / encodeURIComponent

原文地址:https://www.cnblogs.com/yuyutianxia/p/7055597.html