当前位置:   article > 正文

鸿蒙OS开发问题:(ArkTS) 【解决中文乱码 string2Uint8Array、uint8Array2String】_arkts base64编码字符串

arkts base64编码字符串

 在进行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喔

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

运行结果:

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知识←前往

  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 款的应用完成原生鸿蒙开发,这么多的应用需要开发,也就意味着需要有更多的鸿蒙人才。鸿蒙开发工程师也将会迎来爆发式的增长,学习鸿蒙势在必行!

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/424324
推荐阅读
  

闽ICP备14008679号