react-native - 如何在 native react 中将字符串转换为字节数组?

标签 react-native converters

我知道这个问题以不同的方式提出,但我找不到适合 native react 的正确答案......

如何将字符串转换为字节数组我 react 原生?

例如,我希望函数是这样的:

StringToByteArray('你好')

输出应该是这样的:
[72,0,101,0,108,0,108,0,111,0]

我一直在找this发布,但似乎答案不正确或无效...

最佳答案

你应该试试这个解决方案:

 convertStringToByteArray(str){
 String.prototype.encodeHex = function () {
 var bytes = [];
 for (var i = 0; i < this.length; ++i) {
  bytes.push(this.charCodeAt(i));
 }
 return bytes;
 };

 var byteArray = str.encodeHex();
 return byteArray
 }

该功能的使用方法:
var str = "Hello";
console.log("buffer",this.convertStringToByteArray(str));

//output: [ 72, 101, 108, 108, 111 ]

关于react-native - 如何在 native react 中将字符串转换为字节数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54634564/

相关文章:

video - 将 isom 转换为 mp42

react-native - Android *和* iOS 移动应用程序的 Google map 1 API key 限制

ios - 捆绑 index.ios.js 时内置卡在 0.0%

ios - 由于不允许操作, native ios 构建失败

react-native - React Native Flatlist-VirtualizedList : You have a large list that is slow to update

ios - 当我从 XCode 运行应用程序时,React Native : Can't see console. 记录

java - 多框架 GUI 中的 NumberFormatException

java - java中的编码转换

c# - 列为复选框并使用转换器时的无效操作异常

linux - 如何为Linux开发AAC转换器?