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>
相关推荐
皆是梦1 小时前
notepad++ 多行复制拼接
notepad++
The_era_achievs_hero2 天前
微信小程序141~150
微信小程序·小程序·notepad++
公子绝3 天前
JAVA学习笔记 使用notepad++开发JAVA-003
java·学习·notepad++·java开发环境
梦仔生信进阶4 天前
【windows办公小助手】比文档编辑器更好用的Notepad++轻量编辑器
编辑器·notepad++
難釋懷5 天前
微信小程序案例 - 本地生活(首页)
微信小程序·生活·notepad++
難釋懷6 天前
微信小程序WXSS 模板样式
微信小程序·小程序·notepad++
高压锅_12208 天前
Cursor+Coze+微信小程序实战: AI春联生成器
人工智能·微信小程序·notepad++
Klaus_Wei8 天前
Notepad++正则表达全解
notepad++·正则表达·notepad搜索·正则搜索
毛毛三由8 天前
基于svga+uniapp的微信小程序动画组件开发指南
微信小程序·uni-app·notepad++
【ql君】qlexcel15 天前
Notepad++ 复制宏、编辑宏的方法
开发语言·javascript·notepad++··宏编辑·宏复制