vue Uint8Array转字符串中文乱码

解决方法:使用fast-text-encoding组件

该组件包括两部分编码和解码

  • TextEncoder:负责将字符串转Uint8Array
  • TextDecoder:负责将Uint8Array转字符串

使用方法

安装:
npm i fast-text-encoding

依赖:

require('fast-text-encoding')
  • 字符串转Uint8Array
const encoder = new TextEncoder()
const view = encoder.encode('€')
console.log(view); // Uint8Array(3) [226, 130, 172]
  • Uint8Array转字符串
let utf8decoder = new TextDecoder(); // default 'utf-8' or 'utf8'

let u8arr = new Uint8Array([240, 160, 174, 183]);
let i8arr = new Int8Array([-16, -96, -82, -73]);
let u16arr = new Uint16Array([41200, 47022]);
let i16arr = new Int16Array([-24336, -18514]);
let i32arr = new Int32Array([-1213292304]);

console.log(utf8decoder.decode(u8arr));
console.log(utf8decoder.decode(i8arr));
console.log(utf8decoder.decode(u16arr));
console.log(utf8decoder.decode(i16arr));
console.log(utf8decoder.decode(i32arr));

你可能感兴趣的:(javascript,Uint8Array)