当前位置:网站首页>【原创】TypeScript字符串utf-8编码解码

【原创】TypeScript字符串utf-8编码解码

2022-06-27 08:06:00 赵庆明老师

定义一个字符串(原始数据)
将其使用utf8编码成一个数组(编码)
再将其解码成一个字符串(还原)

let str1 = "我们①this is a string";

//我们①this is a string
console.log(str1);

const byteArray = new TextEncoder().encode(str1);
//Uint8Array(25) [
// 230, 136, 145, 228, 187, 172,
// 226, 145, 160, 116, 104, 105,
// 115, 32, 105, 115, 32, 97,
// 32, 115, 116, 114, 105, 110,
// 103
//]
console.log(byteArray);

//19
console.log(str1.length);

//25
console.log(byteArray.length);

//我们①this is a string
console.log(new TextDecoder().decode(byteArray));
原网站

版权声明
本文为[赵庆明老师]所创,转载请带上原文链接,感谢
https://blog.csdn.net/u013667796/article/details/125471261