自学内容网 自学内容网

【JS】将UTF-8编码的字节序列转换为字符串

函数封装

export function utf8ToChinese(utf8String) {
  // 将UTF-8编码的字节序列转换为字符串
  const encoder = new TextEncoder();
  const uint8Array = encoder.encode(utf8String);

  // 使用TextDecoder将UTF-8字节序列解码为JavaScript能理解的字符串
  const decoder = new TextDecoder('utf-8');
  return decoder.decode(uint8Array);
}

函数使用

const chinese = utf8ToChinese(`\u66DD\u5149\u6B21\u6570`);
console.log('chinese =', chinese);// 曝光次数

原文地址:https://blog.csdn.net/qq_45677671/article/details/142457549

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!