Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert String to Byte array in react native?

I know this question asked in different ways but I couldn't find the right answer that fit to react native...

How to convert String to Byte array I react native?

For example I would like the function will be like that:

StringToByteArray('hello')

The output should be something like that: [72,0,101,0,108,0,108,0,111,0]

I have been looking in this post, but it seems the answers are incorrect or invalid...

like image 942
JJ Redikes Avatar asked Oct 26 '25 07:10

JJ Redikes


1 Answers

You should try this solution:

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

The way to use this function:

console.log(convertStringToByteArray("Hello"));
// [ 72, 101, 108, 108, 111 ]
like image 50
Tal Shani Avatar answered Oct 29 '25 07:10

Tal Shani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!