HarmonyOS ArkTS实现Uint8Array类型转Map有问题

代码如下:

const temp = 'eyJ0aW1lc3RhbXAiOiIxNzE5NTgzMjk4MDA3IiwiY2hhbGxlbmdlIjoicXpRaFBvbkdkR2FmVDc2d2dBTmFuUnc2UTE4WE0wengtZUdRNlNPb1Z3bmh2X1kiICwiZG93bmxvYWRDZXIiOiAidHJ1ZSIsInVzZXJWZXJpZmljYXRpb24iOiIyIiAsImRldmljZUlEIjoibWF0ZTMwdGVzdCIgLCJhdXRoVHlwZSI6ImF1dGgiICwiYWRkVXZpIjoibnVsbCIgLCJtb2RlbE51bWJlciI6Im51bGwiIH0'

this.serverData = await this.genClientExts(temp);

private async genClientExts( temp:string): Promise<string> {
  let  base64 = new util.Base64Helper();
  // let exts: ArrayList<Extension> = new ArrayList<Extension>();
  let data:Uint8Array = await base64.decode(temp);
  let serverData = uint8ArrayToString(data); // serverData缺少右花括号
  return serverData;
}

function uint8ArrayToString(array: Uint8Array) {
  // 将UTF-8编码转换成Unicode编码
  let out: string = "";
  let index: number = 0;
  let len: number = array.length;
  while (index < len) {
    let character = array[index++];
    switch(character >> 4) {
      case 0:
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
        out += String.fromCharCode(character);
        break;
      case 12:
      case 13:
        out += String.fromCharCode(((character & 0x1F) << 6) | (array[index++] & 0x3F));
        break;
      case 14:
        out += String.fromCharCode(((character & 0x0F) << 12) | ((array[index++] & 0x3F) << 6) | ((array[index++] & 0x3F) << 0));
        break;
      default:
        break;
    }
  }
  return out;
}
HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-util-V5#decodesync9

decodeSync(src: Uint8Array | string, options?: Type): Uint8Array 

Type为BASIC_URL_SAFE可以用来解码不带“=”的字符串,默认是BASIC,解码带“=”的字符串。

分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS stringUint8Array
23浏览 • 2回复 待解决
ArrayBuffer怎么Uint8Array
601浏览 • 1回复 待解决
HarmonyOS Uint8Array16进制
25浏览 • 2回复 待解决
HarmonyOS Uint8Array怎么正确ArrayBuffer
1463浏览 • 1回复 待解决
如何将Uint8ArrayArrayBuffer?
529浏览 • 1回复 待解决
如何将Uint8Array字符串?
607浏览 • 1回复 待解决
HarmonyOS Uint8Array格式字符串的方法
858浏览 • 1回复 待解决
HarmonyOS 录音发送Uint8Array
67浏览 • 1回复 待解决
Uint8Array如何转成ArrayBuffer
1597浏览 • 1回复 待解决
HarmonyOS string怎么与Uint8Array互转?
438浏览 • 1回复 待解决
Uint8Array是@Sendable类吗?
275浏览 • 1回复 待解决
Uint8Array 如何直接转为String or Json
2727浏览 • 1回复 待解决
HarmonyOS 如何将Uint8Array转为ArrayBuffer
34浏览 • 1回复 待解决
Uint8Array 在native层去解析提示过期
621浏览 • 1回复 待解决
如何将字符串转为Uint8Array
812浏览 • 1回复 待解决
Uint8Array和string之间的相互转换
3143浏览 • 1回复 待解决