鸿蒙OS开发问题:(ArkTS) 【解决中文乱码 string2Uint8Array、uint8Array2String】

news/2024/5/20 3:31:18/文章来源:https://blog.csdn.net/m0_62167422/article/details/137090415

 在进行base64编码中,遇到中文如果不进行处理一定会出现乱码

  let result1: string = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(('一二三四五六七八九十123')))LogUtils.i("result1 = " + result1);let result2: string = CryptoJS.enc.Base64.parse(result1).toString(CryptoJS.enc.Utf8)LogUtils.i("result2 = " + result2);

输出结果:

┌────────────────────────────────────────────────────────├1 result1 = 5LiA5LqM5LiJ5Zub5LqU5YWt5LiD5YWr5Lmd5Y2BMTIz
└────────────────────────────────────────────────────────
┌────────────────────────────────────────────────────────
├1 result2 = ä¸äºä¸åäºåÂ
└────────────────────────────────────────────────────────

刚开始在编码的时候就已经出问题了,使用CryptoJS 框架 截止发稿前就一直存在这个问题,后面只有自己手撸工具类:

import util from '@ohos.util';class StringUtils {/*** string转Uint8Array* @param value* @returns*/string2Uint8Array1(value: string): Uint8Array {if (!value) return null;//let textEncoder = new util.TextEncoder();//获取点流并发出 UTF-8 字节流 TextEncoder 的所有实例仅支持 UTF-8 编码return textEncoder.encodeInto(value)}/*** string转Uint8Array* @param value 包含要编码的文本的源字符串* @param dest 存储编码结果的Uint8Array对象实例* @returns 它返回一个包含读取和写入的两个属性的对象*/string2Uint8Array2(value: string, dest: Uint8Array) {if (!value) return null;if (!dest) dest = new Uint8Array(value.length);let textEncoder = new util.TextEncoder();//read:它是一个数值,指定转换为 UTF-8 的字符串字符数。如果 uint8Array 没有足够的空间,这可能小于 src.length(length of source 字符串)。//dest:也是一个数值,指定存储在目标 Uint8Array 对象 Array 中的 UTF-8 unicode 的数量。它总是等于阅读。textEncoder.encodeIntoUint8Array(value, dest)// let result = textEncoder.encodeIntoUint8Array(value, dest)// result.read// result.written}/*** Uint8Array 转  String* @param input*/uint8Array2String(input: Uint8Array) {let textDecoder = util.TextDecoder.create("utf-8", { ignoreBOM: true })return textDecoder.decodeWithStream(input, { stream: false });}/*** ArrayBuffer 转  String* @param input* @returns*/arrayBuffer2String(input: ArrayBuffer) {return this.uint8Array2String(new Uint8Array(input))}
}export default new StringUtils()

示例代码:

let globalPlainText = ""
globalPlainText += "一二三四五六七八九十"globalPlainText += "SDK向DevEco Studio提供全量API,DevEco Studio识别开发者项目中选择的设备形态,找到该设备的支持能力集,筛选支持能力集包含的API并提供API联想"let dealStr = StringUtils.string2Uint8Array1(globalPlainText)let base64Str = base64.encode(dealStr)LogUtils.i("base64 = " + base64Str);//let arr1: ArrayBuffer = base64.decode(base64Str)LogUtils.i("result1 = " + StringUtils.arrayBuffer2String(arr1));
鸿蒙OS开发更多内容↓点击HarmonyOS与OpenHarmony技术
鸿蒙技术文档开发知识更新库gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md在这。或+mau123789学习,是v喔

搜狗高速浏览器截图20240326151547.png

运行结果:

TextEncoder源码(部分API在since 9 已废弃):

/*** The TextDecoder interface represents a text decoder.* The decoder takes the byte stream as the input and outputs the String string.* @syscap SystemCapability.Utils.Lang* @since 7*/class TextEncoder {/*** Encoding format.* @since 7* @syscap SystemCapability.Utils.Lang*/readonly encoding = "utf-8";/*** The textEncoder constructor.* @since 7* @syscap SystemCapability.Utils.Lang*/constructor();/*** The textEncoder constructor.* @since 9* @syscap SystemCapability.Utils.Lang* @param encoding The string for encoding format.* @throws {BusinessError} 401 - The type of encoding must be string.*/constructor(encoding?: string);/*** Returns the result of encoder.* @since 7* @deprecated since 9* @useinstead ohos.util.encodeInto* @syscap SystemCapability.Utils.Lang* @param input The string to be encoded.* @returns Returns the encoded text.*/encode(input?: string): Uint8Array;/*** UTF-8 encodes the input string and returns a Uint8Array containing the encoded bytes.* @since 9* @syscap SystemCapability.Utils.Lang* @param input The string to be encoded.* @returns Returns the encoded text.* @throws {BusinessError} 401 - The type of input must be string.*/encodeInto(input?: string): Uint8Array;/*** Encode string, write the result to dest array.* @since 7* @deprecated since 9* @useinstead ohos.util.encodeIntoUint8Array* @syscap SystemCapability.Utils.Lang* @param input The string to be encoded.* @param dest Decoded numbers in accordance with the format* @returns Returns Returns the object, where read represents* the number of characters that have been encoded, and written* represents the number of bytes occupied by the encoded characters.*/encodeInto(input: string, dest: Uint8Array): {read: number;written: number;};/*** Encode string, write the result to dest array.* @since 9* @syscap SystemCapability.Utils.Lang* @param input The string to be encoded.* @param dest Decoded numbers in accordance with the format* @returns Returns Returns the object, where read represents* the number of characters that have been encoded, and written* represents the number of bytes occupied by the encoded characters.* @throws {BusinessError} 401 - if the input parameters are invalid.*/encodeIntoUint8Array(input: string, dest: Uint8Array): {read: number;written: number;};}

TextDecoder源码(部分API在since 9 已废弃):

    /*** The TextEncoder represents a text encoder that accepts a string as input,* encodes it in UTF-8 format, and outputs UTF-8 byte stream.* @syscap SystemCapability.Utils.Lang* @since 7*/class TextDecoder {/*** The source encoding's name, lowercased.* @since 7* @syscap SystemCapability.Utils.Lang*/readonly encoding: string;/*** Returns `true` if error mode is "fatal", and `false` otherwise.* @since 7* @syscap SystemCapability.Utils.Lang*/readonly fatal: boolean;/*** Returns `true` if ignore BOM flag is set, and `false` otherwise.* @since 7* @syscap SystemCapability.Utils.Lang*/readonly ignoreBOM = false;/*** The textEncoder constructor.* @since 7* @deprecated since 9* @useinstead ohos.util.TextDecoder.create* @syscap SystemCapability.Utils.Lang* @param encoding Decoding format*/constructor(encoding?: string, options?: {fatal?: boolean;ignoreBOM?: boolean;});/*** The textEncoder constructor.* @since 9* @syscap SystemCapability.Utils.Lang*/constructor();/*** Replaces the original constructor to process arguments and return a textDecoder object.* @since 9* @syscap SystemCapability.Utils.Lang* @param encoding Decoding format* @throws {BusinessError} 401 - if the input parameters are invalid.*/static create(encoding?: string, options?: {fatal?: boolean;ignoreBOM?: boolean;}): TextDecoder;/*** Returns the result of running encoding's decoder.* @since 7* @deprecated since 9* @useinstead ohos.util.decodeWithStream* @syscap SystemCapability.Utils.Lang* @param input Decoded numbers in accordance with the format* @returns Return decoded text*/decode(input: Uint8Array, options?: {stream?: false;}): string;/*** Decodes the input and returns a string. If options.stream is true, any incomplete byte sequences occurring* at the end of the input are buffered internally and emitted after the next call to textDecoder.decode().* If textDecoder.fatal is true, decoding errors that occur will result in a TypeError being thrown.* @since 9* @syscap SystemCapability.Utils.Lang* @param input Decoded numbers in accordance with the format* @returns Return decoded text* @throws {BusinessError} 401 - if the input parameters are invalid.*/decodeWithStream(input: Uint8Array, options?: {stream?: boolean;}): string;}

鸿蒙开发岗位需要掌握那些核心要领?

目前还有很多小伙伴不知道要学习哪些鸿蒙技术?不知道重点掌握哪些?为了避免学习时频繁踩坑,最终浪费大量时间的。

自己学习时必须要有一份实用的鸿蒙(Harmony NEXT)资料非常有必要。 这里我推荐,根据鸿蒙开发官网梳理与华为内部人员的分享总结出的开发文档。内容包含了:【ArkTS、ArkUI、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战】等技术知识点。

废话就不多说了,接下来好好看下这份资料。

如果你是一名Android、Java、前端等等开发人员,想要转入鸿蒙方向发展。可以直接领取这份资料辅助你的学习。鸿蒙OpenHarmony知识←前往。下面是鸿蒙开发的学习路线图。

针对鸿蒙成长路线打造的鸿蒙学习文档。鸿蒙(OpenHarmony )学习手册(共计1236页)与鸿蒙(OpenHarmony )开发入门教学视频,帮助大家在技术的道路上更进一步。

其中内容包含:

《鸿蒙开发基础》鸿蒙OpenHarmony知识←前往

  1. ArkTS语言
  2. 安装DevEco Studio
  3. 运用你的第一个ArkTS应用
  4. ArkUI声明式UI开发
  5. .……

《鸿蒙开发进阶》鸿蒙OpenHarmony知识←前往

  1. Stage模型入门
  2. 网络管理
  3. 数据管理
  4. 电话服务
  5. 分布式应用开发
  6. 通知与窗口管理
  7. 多媒体技术
  8. 安全技能
  9. 任务管理
  10. WebGL
  11. 国际化开发
  12. 应用测试
  13. DFX面向未来设计
  14. 鸿蒙系统移植和裁剪定制
  15. ……

《鸿蒙开发实战》鸿蒙OpenHarmony知识←前往

  1. ArkTS实践
  2. UIAbility应用
  3. 网络案例
  4. ……

最后

鸿蒙是完全具备无与伦比的机遇和潜力的;预计到年底将有 5,000 款的应用完成原生鸿蒙开发,这么多的应用需要开发,也就意味着需要有更多的鸿蒙人才。鸿蒙开发工程师也将会迎来爆发式的增长,学习鸿蒙势在必行!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_1025791.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mac-git上传至github(ssh版本,个人tokens总出错)

第一步 git clone https://github.com/用户名/项目名.git 第二步 cd 项目名 第三步 将本地的文件移动到项目下 第四步 git add . 第五步 git commit -m "添加****文件夹" 第六步 git push origin main 报错: 采用ssh验证 本地文件链接公钥 …

软件杯 深度学习+opencv+python实现车道线检测 - 自动驾驶

文章目录 0 前言1 课题背景2 实现效果3 卷积神经网络3.1卷积层3.2 池化层3.3 激活函数:3.4 全连接层3.5 使用tensorflow中keras模块实现卷积神经网络 4 YOLOV56 数据集处理7 模型训练8 最后 0 前言 🔥 优质竞赛项目系列,今天要分享的是 &am…

电脑windows 蓝屏【恢复—无法加载操作系统,原因是关键系统驱动程序丢失或包含错误。.......】

当你碰到下图这种情况的电脑蓝屏,先别急着重装系统,小编本来也是想重装系统的,但是太麻烦,重装系统后你还得重装各种软件,太麻烦了!! 这种情况下,你就拿出你的启动U盘,进…

OSCP靶场--GLPI

OSCP靶场–GLPI 考点(CVE-2022-35914 php执行函数绕过ssh端口转发jetty xml RCE) 1.nmap扫描(ssh端口转发) ## ┌──(root㉿kali)-[~/Desktop] └─# nmap 192.168.194.242 -sV -sC --min-rate 2500 Starting Nmap 7.92 ( https://nmap.org ) at 2024-03-26 22:22 EDT Nmap…

快速上手Spring Cloud 十一:微服务架构下的安全与权限管理

快速上手Spring Cloud 一:Spring Cloud 简介 快速上手Spring Cloud 二:核心组件解析 快速上手Spring Cloud 三:API网关深入探索与实战应用 快速上手Spring Cloud 四:微服务治理与安全 快速上手Spring Cloud 五:Spring …

python opencv稍基础初学

傅里叶变换 傅里叶变换f​​​​​傅里叶分析之掐死教程(完整版)更新于2014.06.06 - 知乎 (zhihu.com)https://zhuanlan.zhihu.com/p/19763358 相当nice 傅里叶变换的作用 高频:变化剧烈的灰度分量,例如边界 低频:变…

【搜索引擎2】实现API方式调用ElasticSearch8接口

1、理解ElasticSearch各名词含义 ElasticSearch对比Mysql Mysql数据库Elastic SearchDatabase7.X版本前有Type,对比数据库中的表,新版取消了TableIndexRowDocumentColumnmapping Elasticsearch是使用Java开发的,8.1版本的ES需要JDK17及以上…

Elasticsearch-相关性

相关性描述的是⼀个⽂档和查询语句匹配的程度。ES 会对每个匹配查询条件的结果进⾏算分_score。_score 的评分越高,相关度越高。 ES 5.0之前使用TF-IDF 相关性算法, 5.0之后使用了BM25算法 TF-IDF 公式 score(q,d) queryNorm(q) coord(q,d) …

数据处理库Pandas数据结构DataFrame

Dataframe是一种二维数据结构,数据以表格形式(与Excel类似)存储,有对应的行和列,如图3-3所示。它的每列可以是不同的值类型(不像 ndarray 只能有一个 dtype)。基本上可以把 DataFrame 看成是共享…

@EnableWebMvc 导致自定义序列化器失效

目录 前言 一. 自定义序列化器失效 1.1 EnableWebMvc 的作用 1.2 EnableWebMvc 带来了什么后果 1.3 原理分析 1.4 问题解决 二. 总结 前言 在使用Swagger的时候用 到了EnableWebMvc,发现之前为了解决Long类型、日期类型等自定义序列化器失效了 Configurati…

基于javaweb宠物领养平台管理系统设计和实现

基于javaweb宠物领养平台管理系统设计和实现 博主介绍:多年java开发经验,专注Java开发、定制、远程、文档编写指导等,csdn特邀作者、专注于Java技术领域 作者主页 央顺技术团队 Java毕设项目精品实战案例《1000套》 欢迎点赞 收藏 ⭐留言 文末获取源码联…

Android ddms在macOS上面卡死和Java版本异常无法关闭弹窗处理

背景 在macOS上面打开ddms工具遇到错误。产留的uix文件无法打开,弹出无法关闭和进入ddms无任何响应。 问题-无法关闭的弹窗 首先ddms在Android SDK中位置/sdk/tools/monitor这个二进制文件就是ddms程序了。 终端执行这个程序即可。第一个遇到的问题,打开ddms之后,弹出一个…

MySQL 高级语句(二)

一、子查询 1.1 相同表子查询 1.2 不同表/多表子查询 1.3 子查询的应用 1.3.1 语法 1.3.2 insert 子查询 1.3.3 update 子查询 1.3.4 delete 子查询 1.4 exists 关键字 1.4.1 true 1.4.2 false 1.5 as别名 二、视图 2.1 视图和表的区别和联系 2.1.1 区别 2.1.2 …

阿里云云服务器资源规格推荐指南

资源规格推荐可以根据您的特定业务场景,为您推荐最合适的计算资源规格以及满足您算力需求的资源规模。本文介绍如何根据物理机规格推荐ECS资源和根据总算力推荐ECS资源。 根据物理机规格推荐ECS资源 IDC上云可以帮助您在将线下IDC服务器搬迁上云前,根据…

单臂路由和三层交换机

目录 一.单臂路由 1.单臂路由的工作原理 2.单臂路由的配置 2.1画出拓扑图 2.2配置PC 2.3配置交换机 2.4配置路由器 2.5测试 二.三层交换机 1.三层交换机的概述 2.三层交换机的配置 2.1画出拓扑图 2.2配置PC 2.3配置二层交换机 2.4配置三层交换机 2.5测试 3.拓展 三.总结 一.…

iOS_convert point or rect 坐标和布局转换+判断

文章目录 1. 坐标转换2. 布局转换3. 包含、相交 如:有3个色块 let view1 UIView(frame: CGRect(x: 100.0, y: 100.0, width: 300.0, height: 300.0)) view1.backgroundColor UIColor.cyan self.view.addSubview(view1)let view2 UIView(frame: CGRect(x: 50.0, …

AI视频渲染原理是什么?

一、AI渲染原理 AI视频渲染是一种结合了人工智能技术的新型渲染方式,它主要通过深度学习和其他机器学习方法来优化传统渲染流程,以提高效率和质量。以下是AI视频渲染可能涉及的一些基本原理: 1. **智能采样**: - AI可以帮助决定在…

Git使用:实现文件在不同设备之间进行同步

一、注册Gitee,创建远程仓库 注册网址:登录 - Gitee.com 打开Gitee,注册完进行登录,点击右上角【】创建一个仓库 新建仓库: 点击创建,仓库创建完毕。 二、下载Git安装包,并创建本地仓库 下载网…

STM32串口收发单字节数据原理及程序实现

线路连接: 显示屏的SCA接在B11,SCL接在B10,串口的RX连接A9,TX连接A10。 程序编写: 在上一个博客中实现了串口的发送代码,这里实现串口的接收代码,在上一个代码的基础上增加程序功能。 Seiral.…

【数字图像处理matlab系列】数组索引

【数字图像处理matlab系列】数组索引 【先赞后看养成习惯】【求点赞+关注+收藏】 MATLAB 支持大量功能强大的索引方案,这些索引方案不仅简化了数组操作,而且提高了程序的运行效率。 1. 向量索引 维数为1xN的数组称为行向量。行向量中元素的存取是使用一维索引进行的。因此…