16进制字符串转字符串

一、浏览器上

c 复制代码
function hexToUtf8(hexString) {
  const hexArray = hexString.match(/.{1,2}/g) || [];
  const uint8Array = new Uint8Array(hexArray.map(hex => parseInt(hex, 16)));

  const textDecoder = new TextDecoder('GB2312'); //可以切换字符编码
  return textDecoder.decode(uint8Array);
}

// 示例用法
const hexString = "0D0A2D2D2D20B6C1C8A1B6A8CAB1C9CFB1A8B2CECAFD202D2D2D0D0A"; // 16进制字符串
const result = hexToUtf8(hexString);
console.log(result); // 输出: "hello, 你好"

在微信小程序中提示 TextDecoder is not defined,微信小程序环境中可能没有内置的TextDecoder

二、微信小程序上

在微信小程序中,如果你需要指定编码格式为GB2312,可以使用一个库来进行解码,因为微信小程序环境并没有原生支持GB2312编码。你可以考虑使用一个名为 iconv-lite 的库,该库支持多种字符编码的转换。

1、首先,在你的小程序项目中安装 iconv-lite 库:

c 复制代码
npm install iconv-lite

2、然后,使用以下代码进行解码:

c 复制代码
const iconv = require('iconv-lite');

function hexToGb2312(hexString) {
  const buffer = Buffer.from(hexString, 'hex');
  const decodedString = iconv.decode(buffer, 'GB2312');
  return decodedString;
}

// 示例用法
const hexString = "0D0A2D2D2D20B6C1C8A1B6A8CAB1C9CFB1A8B2CECAFD202D2D2D0D0A"; // 16进制字符串
const result = hexToGb2312(hexString);
console.log(result); // 输出: "hello, 你好"

3、然在再在页面中显示

由于字符串中有\r\n,标签并不能使\r\n 换行,要 标签才能进行换行

c 复制代码
<text class="result_content"> 
				{{logChina}}
			</text>
相关推荐
x***r15115 小时前
Notepad++ 6.6.9安装步骤详解(附Notepad++离线安装教程)
notepad++
Uso_Magic4 天前
Notepad++ 批量光标编辑方法。
notepad++
秋99 天前
Notepad++ 完全使用手册:从入门到精通
notepad++
sanqima10 天前
设置Notepad++在cpp文件里的字体与样式
notepad++·字体与颜色
特立独行的猫a11 天前
HarmonyOS鸿蒙PC开源QT软件移植:移植开源文本编辑器 NotePad--(Ndd)到鸿蒙 PC实践总结
qt·开源·notepad++·harmonyos·notepad--·鸿蒙pc
qq_4335021813 天前
微信小程序更新机制踩坑记录:updateInfo 为什么总是读到旧数据?
微信小程序·小程序·notepad++
嵌入式×边缘AI:打怪升级日志16 天前
嵌入式Linux应用开发快速入门(从零到第一个程序)
linux·运维·notepad++
LXXgalaxy17 天前
微信小程序“记住密码”功能的实现与落地 vue3+ts的细致解析
微信小程序·小程序·notepad++
克里斯蒂亚诺更新17 天前
微信小程序 腾讯地图 点聚合 简单示例
微信小程序·小程序·notepad++
人还是要有梦想的19 天前
如何开发小程序介绍
小程序·notepad++