HarmonyOS 鸿蒙Next arkTs /js/ts 怎么将字符串转gb2312数组

HarmonyOS 鸿蒙Next arkTs /js/ts 怎么将字符串转gb2312数组

java 是 这么转换的,byte[] nameD = name.getBytes(“GB2312”);


OpenHarmony上,在ts上面要怎么将字符串转为gb2312数组?
2 回复

https://blog.csdn.net/u013866683/article/details/122193183

试一下这个,或者HarmonyOS的库你可以去看看,可能有

更多关于HarmonyOS 鸿蒙Next arkTs /js/ts 怎么将字符串转gb2312数组的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS(鸿蒙)的ArkTS/JS/TS中,可以通过TextEncoder将字符串转换为指定编码的字节数组。对于GB2312编码,鸿蒙系统目前没有直接支持,但可以通过第三方库或自定义编码表实现。

以下是一个简单的示例,展示如何将字符串转换为GB2312编码的字节数组:

function stringToGB2312Array(str: string): Uint8Array {
    const encoder = new TextEncoder('gb2312');
    return encoder.encode(str);
}

const str = "你好,鸿蒙";
const gb2312Array = stringToGB2312Array(str);
console.log(gb2312Array);

需要注意的是,TextEncoder默认支持UTF-8编码,如果需要支持GB2312,可能需要自定义编码逻辑或使用第三方库。

回到顶部