npm install text-encoding --save
import {
TextEncoder,
TextDecoder
} from 'text-encoding'
/**
* 编码
* @param {*} str 需要编码的字符串
* @param {*} encoding 编码类型(gb2312,utf-8)
* @returns unit8Array类型的对象
*/
export function encode(str, encoding) {
let encode = new TextEncoder(encoding, {
NONSTANDARD_allowLegacyEncoding: true
})
let uint8Array = encode.encode(str);
return uint8Array;
}
/**
* 解码
* @param {*} uint8Array 需要解码的unit8Array类型的对象
* @param {*} encoding 解码类型(gb2312,utf-8)
* @returns 解码出来的字符串
*/
export function decode(uint8Array, encoding) {
let decode = new TextDecoder(encoding);
return decode.decode(uint8Array)
}
注1:编码出来的是unit8Array对象,解码的时候需要new Uint8Array(arr)一个字节数组生成
注2:encoding是解码类型,gbk填gb2312
网友评论