JavaScript UTF-8编码转换

JavaScript UTF-8编码问题

今天在开发中遇到一个问题,在JavaScript中向服务器发送数据时,由于一些限制,只能发送经过编码的字节流数据,本来打算直接用JavaScript里的String.charCodeAt()和String.fromCharCode()来实现编码转换,结果在转换中文字符时发生了问题,中文自动转换后再转为Uint8Array,会丢失数据,不得已只能自己写了个字符串转UTF-8的函数,直接分享给大家吧。

字符串转UTF-8字节数组

stringToUTF8Array.

function stringToUTF8Array(str) {
  if (!str)
    return false;
  let result = [];
  for (let i = 0, j = str.length; i < j; i++) {
    let code = str.charCodeAt(i);
    if (code <= 0x7f) {
      result.push(code);
    } else if (code <= 0x7ff) {
      result.push((0xC0 | (0x1F & (code >> 6))));
      result.push((0x80 | (0x3F & code)));
    } else if (code <= 0xffff) {
      result.push((0xE0 | (0x0F & (code >> 12))));
      result.push((0x80 | (0x3F & (code >> 6))));
      result.push((0x80 | (0x3F & code)));
    } else {
      return false;
    }
  }
  return Uint8Array.from(result);
}

UTF-8字节数组转字符串

utf8ArrayToString.

function utf8ArrayToString(array) {
  if(!array)
  return false;
  let result = "";
  for (let i = 0, j = array.length; i < j; i++) {
    let code = array[i];
    if (code >= 0 && code <= 0x7f) {
      code = (0x7f & code);
    } else if (code <= 0xdf) {
      code = ((0x1F & array[i]) << 6) | (0x3f & array[i + 1]);
      i += 1;
    } else if (code <= 0xef) {
      code = ((0x0f & array[i]) << 12) | ((0x3f & array[i + 1]) << 6) | (0x3f & array[i + 2]);
      i += 2;
    } else {
      return false;
    }
    let char = String.fromCharCode(code);
    result += char;
  }
  return result;
}

这里部分思路借鉴了
https://www.cnblogs.com/doublenet/p/5616451.html
这篇文章。

你可能感兴趣的:(JavaScript)