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>
相关推荐
idolao5 小时前
npp.8.5.Installer文本编辑器安装步骤详解(附Notepad++配置与插件安装教程)
notepad++
px不是xp1 天前
【灶台导航】 RAG系统的容错设计:从向量搜索到关键词降级,一个都不能少
javascript·微信小程序·notepad++·rag
空中海2 天前
微信小程序 - 03 工程实践层与综合 Demo
微信小程序·小程序·notepad++
舟遥遥娓飘飘4 天前
面向零基础初学者,从环境搭建到发布上线,手把手教你开发第一个微信小程序(第3章-认识项目结构)
微信小程序·小程序·notepad++
优睿远行4 天前
微信小程序自定义组件开发实战:从封装到发布的全流程指南
微信小程序·小程序·notepad++
喜欢南方姑娘7 天前
微信小程序热更新-用户打开小程序时检测版本自动更新
微信小程序·小程序·notepad++
yzx9910137 天前
从零开始写一个微信小程序:完整代码实战指南(入门篇)
微信小程序·小程序·notepad++
fengyehongWorld8 天前
Notepad++ NppExec插件的使用
notepad++
fengyehongWorld10 天前
Notepad++ 常用插件
notepad++
fengyehongWorld10 天前
Notepad++ PythonScript插件,添加自定义文本转换功能
notepad++