参考示例如下: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%') } }
参考示例如下: