鸿蒙OS开发问题:(ArkTS) 【解决中文乱码 string2Uint8Array、uint8Array2S
在举行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喔
https://img-blog.csdnimg.cn/img_convert/a99a78cf47b6afd10f0dab8e634caf77.png
运行结果:
https://img-blog.csdnimg.cn/img_convert/64b7f58b8be78e089be12be10b57d6c5.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知识←前往。下面是鸿蒙开发的学习门路图。
https://img-blog.csdnimg.cn/img_convert/1d37453a6210a1104576ba867498219b.png
https://img-blog.csdnimg.cn/img_convert/f68b2bfb9d570beea9dc3624efe46e21.png
https://img-blog.csdnimg.cn/img_convert/6c3c59abf7036e13c8ac9fcc05d34c80.png
针对鸿蒙成长门路打造的鸿蒙学习文档。鸿蒙(OpenHarmony )学习手册(共计1236页)与鸿蒙(OpenHarmony )开发入门讲授视频,帮助大家在技能的道路上更进一步。
其中内容包含:
《鸿蒙开发根本》鸿蒙OpenHarmony知识←前往
[*]ArkTS语言
[*]安装DevEco Studio
[*]运用你的第一个ArkTS应用
[*]ArkUI声明式UI开发
[*].……
《鸿蒙开发进阶》鸿蒙OpenHarmony知识←前往
[*]Stage模型入门
[*]网络管理
[*]数据管理
[*]电话服务
[*]分布式应用开发
[*]通知与窗口管理
[*]多媒体技能
[*]安全技能
[*]任务管理
[*]WebGL
[*]国际化开发
[*]应用测试
[*]DFX面向未来计划
[*]鸿蒙体系移植和裁剪定制
[*]……
《鸿蒙开发实战》鸿蒙OpenHarmony知识←前往
[*]ArkTS实践
[*]UIAbility应用
[*]网络案例
[*]……
末了
鸿蒙是完全具备无与伦比的机遇和潜力的;预计到年底将有 5,000 款的应用完成原生鸿蒙开发,这么多的应用必要开发,也就意味着必要有更多的鸿蒙人才。鸿蒙开发工程师也将会迎来爆发式的增长,学习鸿蒙势在必行!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]