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