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...
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 ]
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With