0
点赞
收藏
分享

微信扫一扫

nodejs汉字转Unicode编码实现


在nodejs中,可以使用charCodeAt()fromCharCode()方法将汉字转换为Unicode编码,也可以使用codePointAt()String.fromCodePoint()方法实现。下面分别介绍这两种方法的实现和详细介绍。

  • 1.使用charCodeAt()fromCharCode()方法

function chineseToUnicode(str) {
  let result = '';
  for (let i = 0; i < str.length; i++) {
    let charCode = str.charCodeAt(i).toString(16);
    result += '\\u' + charCode;
  }
  return result;
}

function unicodeToChinese(str) {
  return unescape(str.replace(/\\u/g, '%u'));
}

let chineseStr = '你好,世界!';
let unicodeStr = chineseToUnicode(chineseStr);
console.log('Unicode编码:', unicodeStr); // Unicode编码: \u4f60\u597d\uff0c\u4e16\u754c\uff01

let decodedStr = unicodeToChinese(unicodeStr);
console.log('解码后的汉字:', decodedStr); // 解码后的汉字: 你好,世界!

charCodeAt()方法可以获取字符串中指定位置的字符的Unicode编码,fromCharCode()方法可以将Unicode编码转换为对应的字符。

  • 2.使用codePointAt()String.fromCodePoint()方法

function chineseToUnicode(str) {
  let result = '';
  for (let i = 0; i < str.length; i++) {
    let codePoint = str.codePointAt(i).toString(16);
    result += '\\u' + codePoint;
  }
  return result;
}

function unicodeToChinese(str) {
  return String.fromCodePoint(...str.split('\\u').slice(1).map(code => parseInt(code, 16)));
}

let chineseStr = '你好,世界!';
let unicodeStr = chineseToUnicode(chineseStr);
console.log('Unicode编码:', unicodeStr); // Unicode编码: \u4f60\u597d\uff0c\u4e16\u754c\uff01

let decodedStr = unicodeToChinese(unicodeStr);
console.log('解码后的汉字:', decodedStr); // 解码后的汉字: 你好,世界!

codePointAt()方法可以获取字符串中指定位置的字符的Unicode编码点,String.fromCodePoint()方法可以将一个或多个Unicode编码点转换为对应的字符。

注意:在实际开发中,如果需要处理非BMP(Basic Multilingual Plane,基本多文字平面)的Unicode字符(如emoji表情、某些特殊符号等),建议使用第二种方法,因为charCodeAt()方法只能处理BMP平面中的字符。

我们也可借助在线工具实现汉字转Unicode编码,比如:在线Unicode编码转换

举报

相关推荐

0 条评论