首页 技术 正文
技术 2022年11月6日
0 收藏 991 点赞 973 浏览 1132 个字

String.prototype.charCodeAt

String.fromCharCode()

String.prototype.toUtfArray = function() {
return this.split('').reduce(function(a, c) {
var code = c.charCodeAt(0);
a.push(code >> 8);
a.push(code & 0x0ff);
return a;
}, []);
};
String.fromUtfArray = function(a) {
// 长度偶数个 否则前面补0
if (a.length % 2 ===1) {
a.unshift(0);
}
var wa = [], code = 0;
for (var i = 0; i< a.length; i++) {
if (i%2===0) {
code = a[i] << 8;
} else {
code |= a[i];
wa.push(code);
}
}
return wa.map(function(c) {
return String.fromCharCode(c);
}).join('');
};

  

test:

var s1 = "你好a";
var a = s1.toUtfArray(); // [79, 96, 89, 125, 0, 97]
console.log(a.toString());
var s2 = String.fromUtfArray(a);
console.log(s2); // "你好a"

* UTF-8 变长

字符            UTF-8编码    Byte 1                   Byte 2             Byte 3

A                                   01000001

Ö                                  11000011            10010110

中                                 11100100            10111000      10101101

———————————————————

Binary    Hex                 Comments
0xxxxxxx   0x00..0x7F   Only byte of a 1-byte character encoding
10xxxxxx   0x80..0xBF   Continuation bytes (1-3 continuation bytes)
110xxxxx    0xC0..0xDF   First byte of a 2-byte character encoding
1110xxxx    0xE0..0xEF   First byte of a 3-byte character encoding
11110xxx    0xF0..0xF7   First byte of a 4-byte character encoding

  

上一篇: eps出坑出坑
下一篇: postman 插件安装
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,116
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,588
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,434
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,204
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,840
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,925