HarmonyOS 如何实现有符号的数组?

如题:HarmonyOS 如何实现有符号的数组?

阅读 510
1 个回答

参考示例如下:

import util from '@ohos.util';

function stringToSignedBytes(str: string): Int8Array {
  const encoder = new util.TextEncoder();
  const uint8Array = encoder.encodeInto(str);
  return new Int8Array(uint8Array);
}

@Entry
@Component
struct Index4 {
  @State message: string = 'Hello World';

  aboutToAppear(): void {
    let str: string = "字符转换问题";
    let signedBytes: Int8Array = stringToSignedBytes(str);
    console.log(signedBytes.toString());
    this.message = signedBytes.toString()
  }

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('Index4HelloWorld')
        .fontSize(20)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
    }
    .height('100%')
    .width('100%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏