欢迎关注微信公众号:数据科学与艺术 作者WX:superhe199
在ArkTS中,可以使用以下方法将字符串转换为字节数组:
const str = "Hello, World!";
const bytes = new TextEncoder().encode(str);
使用了TextEncoder
类中的encode
方法,该方法将字符串编码为字节数组。代码中,将字符串"Hello, World!"
编码为字节数组bytes
。
TextEncoder是一个全局对象,可以直接使用而无需额外的导入。