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

打印 上一主题 下一主题

主题 1734|帖子 1734|积分 5202

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
 在举行base64编码中,遇到中文假如不举行处置惩罚肯定会出现乱码
  1.   let result1: string = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(('一二三四五六七八九十123')))
  2.   LogUtils.i("result1 = " + result1);
  3.   let result2: string = CryptoJS.enc.Base64.parse(result1).toString(CryptoJS.enc.Utf8)
  4.   LogUtils.i("result2 = " + result2);
复制代码
输出结果:

  1. ┌────────────────────────────────────────────────────────
  2. ├1 result1 = 5LiA5LqM5LiJ5Zub5LqU5YWt5LiD5YWr5Lmd5Y2BMTIz
  3. └────────────────────────────────────────────────────────
  4. ┌────────────────────────────────────────────────────────
  5. ├1 result2 = ä¸äºä¸åäºåÂ
  6. └────────────────────────────────────────────────────────
复制代码
刚开始在编码的时候就已经出问题了,利用CryptoJS 框架 截止发稿前就不绝存在这个问题,后面只有自己手撸工具类:
  1. import util from '@ohos.util';
  2. class StringUtils {
  3.   /**
  4.    * string转Uint8Array
  5.    * @param value
  6.    * @returns
  7.    */
  8.   string2Uint8Array1(value: string): Uint8Array {
  9.     if (!value) return null;
  10.     //
  11.     let textEncoder = new util.TextEncoder();
  12.     //获取点流并发出 UTF-8 字节流 TextEncoder 的所有实例仅支持 UTF-8 编码
  13.     return textEncoder.encodeInto(value)
  14.   }
  15.   /**
  16.    * string转Uint8Array
  17.    * @param value 包含要编码的文本的源字符串
  18.    * @param dest 存储编码结果的Uint8Array对象实例
  19.    * @returns 它返回一个包含读取和写入的两个属性的对象
  20.    */
  21.   string2Uint8Array2(value: string, dest: Uint8Array) {
  22.     if (!value) return null;
  23.     if (!dest) dest = new Uint8Array(value.length);
  24.     let textEncoder = new util.TextEncoder();
  25.     //read:它是一个数值,指定转换为 UTF-8 的字符串字符数。如果 uint8Array 没有足够的空间,这可能小于 src.length(length of source 字符串)。
  26.     //dest:也是一个数值,指定存储在目标 Uint8Array 对象 Array 中的 UTF-8 unicode 的数量。它总是等于阅读。
  27.     textEncoder.encodeIntoUint8Array(value, dest)
  28.     // let result = textEncoder.encodeIntoUint8Array(value, dest)
  29.     // result.read
  30.     // result.written
  31.   }
  32.   /**
  33.    * Uint8Array 转  String
  34.    * @param input
  35.    */
  36.   uint8Array2String(input: Uint8Array) {
  37.     let textDecoder = util.TextDecoder.create("utf-8", { ignoreBOM: true })
  38.     return textDecoder.decodeWithStream(input, { stream: false });
  39.   }
  40.   /**
  41.    * ArrayBuffer 转  String
  42.    * @param input
  43.    * @returns
  44.    */
  45.   arrayBuffer2String(input: ArrayBuffer) {
  46.     return this.uint8Array2String(new Uint8Array(input))
  47.   }
  48. }
  49. export default new StringUtils()
复制代码
示例代码:

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

运行结果:


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

  1. /**
  2.      * The TextDecoder interface represents a text decoder.
  3.      * The decoder takes the byte stream as the input and outputs the String string.
  4.      * @syscap SystemCapability.Utils.Lang
  5.      * @since 7
  6.      */
  7.     class TextEncoder {
  8.         /**
  9.          * Encoding format.
  10.          * @since 7
  11.          * @syscap SystemCapability.Utils.Lang
  12.          */
  13.         readonly encoding = "utf-8";
  14.         /**
  15.          * The textEncoder constructor.
  16.          * @since 7
  17.          * @syscap SystemCapability.Utils.Lang
  18.          */
  19.         constructor();
  20.         /**
  21.          * The textEncoder constructor.
  22.          * @since 9
  23.          * @syscap SystemCapability.Utils.Lang
  24.          * @param encoding The string for encoding format.
  25.          * @throws {BusinessError} 401 - The type of encoding must be string.
  26.          */
  27.         constructor(encoding?: string);
  28.         /**
  29.          * Returns the result of encoder.
  30.          * @since 7
  31.          * @deprecated since 9
  32.          * @useinstead ohos.util.encodeInto
  33.          * @syscap SystemCapability.Utils.Lang
  34.          * @param input The string to be encoded.
  35.          * @returns Returns the encoded text.
  36.          */
  37.         encode(input?: string): Uint8Array;
  38.         /**
  39.          * UTF-8 encodes the input string and returns a Uint8Array containing the encoded bytes.
  40.          * @since 9
  41.          * @syscap SystemCapability.Utils.Lang
  42.          * @param input The string to be encoded.
  43.          * @returns Returns the encoded text.
  44.          * @throws {BusinessError} 401 - The type of input must be string.
  45.          */
  46.         encodeInto(input?: string): Uint8Array;
  47.         /**
  48.          * Encode string, write the result to dest array.
  49.          * @since 7
  50.          * @deprecated since 9
  51.          * @useinstead ohos.util.encodeIntoUint8Array
  52.          * @syscap SystemCapability.Utils.Lang
  53.          * @param input The string to be encoded.
  54.          * @param dest Decoded numbers in accordance with the format
  55.          * @returns Returns Returns the object, where read represents
  56.          * the number of characters that have been encoded, and written
  57.          * represents the number of bytes occupied by the encoded characters.
  58.          */
  59.         encodeInto(input: string, dest: Uint8Array): {
  60.             read: number;
  61.             written: number;
  62.         };
  63.         /**
  64.          * Encode string, write the result to dest array.
  65.          * @since 9
  66.          * @syscap SystemCapability.Utils.Lang
  67.          * @param input The string to be encoded.
  68.          * @param dest Decoded numbers in accordance with the format
  69.          * @returns Returns Returns the object, where read represents
  70.          * the number of characters that have been encoded, and written
  71.          * represents the number of bytes occupied by the encoded characters.
  72.          * @throws {BusinessError} 401 - if the input parameters are invalid.
  73.          */
  74.         encodeIntoUint8Array(input: string, dest: Uint8Array): {
  75.             read: number;
  76.             written: number;
  77.         };
  78.     }
复制代码
TextDecoder源码(部分API在since 9 已废弃):
  1.     /**
  2.      * The TextEncoder represents a text encoder that accepts a string as input,
  3.      * encodes it in UTF-8 format, and outputs UTF-8 byte stream.
  4.      * @syscap SystemCapability.Utils.Lang
  5.      * @since 7
  6.      */
  7.     class TextDecoder {
  8.         /**
  9.          * The source encoding's name, lowercased.
  10.          * @since 7
  11.          * @syscap SystemCapability.Utils.Lang
  12.          */
  13.         readonly encoding: string;
  14.         /**
  15.          * Returns `true` if error mode is "fatal", and `false` otherwise.
  16.          * @since 7
  17.          * @syscap SystemCapability.Utils.Lang
  18.          */
  19.         readonly fatal: boolean;
  20.         /**
  21.          * Returns `true` if ignore BOM flag is set, and `false` otherwise.
  22.          * @since 7
  23.          * @syscap SystemCapability.Utils.Lang
  24.          */
  25.         readonly ignoreBOM = false;
  26.         /**
  27.          * The textEncoder constructor.
  28.          * @since 7
  29.          * @deprecated since 9
  30.          * @useinstead ohos.util.TextDecoder.create
  31.          * @syscap SystemCapability.Utils.Lang
  32.          * @param encoding Decoding format
  33.          */
  34.         constructor(encoding?: string, options?: {
  35.             fatal?: boolean;
  36.             ignoreBOM?: boolean;
  37.         });
  38.         /**
  39.          * The textEncoder constructor.
  40.          * @since 9
  41.          * @syscap SystemCapability.Utils.Lang
  42.          */
  43.         constructor();
  44.         /**
  45.          * Replaces the original constructor to process arguments and return a textDecoder object.
  46.          * @since 9
  47.          * @syscap SystemCapability.Utils.Lang
  48.          * @param encoding Decoding format
  49.          * @throws {BusinessError} 401 - if the input parameters are invalid.
  50.          */
  51.         static create(encoding?: string, options?: {
  52.             fatal?: boolean;
  53.             ignoreBOM?: boolean;
  54.         }): TextDecoder;
  55.         /**
  56.          * Returns the result of running encoding's decoder.
  57.          * @since 7
  58.          * @deprecated since 9
  59.          * @useinstead ohos.util.decodeWithStream
  60.          * @syscap SystemCapability.Utils.Lang
  61.          * @param input Decoded numbers in accordance with the format
  62.          * @returns Return decoded text
  63.          */
  64.         decode(input: Uint8Array, options?: {
  65.             stream?: false;
  66.         }): string;
  67.         /**
  68.          * Decodes the input and returns a string. If options.stream is true, any incomplete byte sequences occurring
  69.          * at the end of the input are buffered internally and emitted after the next call to textDecoder.decode().
  70.          * If textDecoder.fatal is true, decoding errors that occur will result in a TypeError being thrown.
  71.          * @since 9
  72.          * @syscap SystemCapability.Utils.Lang
  73.          * @param input Decoded numbers in accordance with the format
  74.          * @returns Return decoded text
  75.          * @throws {BusinessError} 401 - if the input parameters are invalid.
  76.          */
  77.         decodeWithStream(input: Uint8Array, options?: {
  78.             stream?: boolean;
  79.         }): string;
  80.     }
复制代码

鸿蒙开发岗位必要掌握那些焦点要领?

目前另有许多小搭档不知道要学习哪些鸿蒙技能?不知道重点掌握哪些?为了制止学习时频繁踩坑,终极浪费大量时间的。
自己学习时必须要有一份实用的鸿蒙(Harmony NEXT)资料非常有必要。 这里我推荐,根据鸿蒙开发官网梳理与华为内部人员的分享总结出的开发文档。内容包含了:【ArkTS、ArkUI、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技能、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战】等技能知识点。
废话就不多说了,接下来好悦目下这份资料。
假如你是一名Android、Java、前端等等开发人员,想要转入鸿蒙方向发展。可以直接领取这份资料辅助你的学习。鸿蒙OpenHarmony知识←前往。下面是鸿蒙开发的学习门路图。






针对鸿蒙成长门路打造的鸿蒙学习文档。鸿蒙(OpenHarmony )学习手册(共计1236页)与鸿蒙(OpenHarmony )开发入门讲授视频,帮助大家在技能的道路上更进一步。
其中内容包含:

《鸿蒙开发根本》鸿蒙OpenHarmony知识←前往

  • ArkTS语言
  • 安装DevEco Studio
  • 运用你的第一个ArkTS应用
  • ArkUI声明式UI开发
  • .……
《鸿蒙开发进阶》鸿蒙OpenHarmony知识←前往

  • Stage模型入门
  • 网络管理
  • 数据管理
  • 电话服务
  • 分布式应用开发
  • 通知与窗口管理
  • 多媒体技能
  • 安全技能
  • 任务管理
  • WebGL
  • 国际化开发
  • 应用测试
  • DFX面向未来计划
  • 鸿蒙体系移植和裁剪定制
  • ……
《鸿蒙开发实战》鸿蒙OpenHarmony知识←前往

  • ArkTS实践
  • UIAbility应用
  • 网络案例
  • ……
末了

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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

郭卫东

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表