How to convert a JavaScript string to byte array using ASCII encoding?
In C#, it is done as:
var encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(string);
I want to do the same in JavaScript for my nodejs server
For Node.js this is fairly easy:
var keyByte = new Buffer(string, "ascii");
Buffer is a container of bytes, and can be treated as an array:
var bytes = new Buffer("Hello, world", "ascii");
console.log(bytes[3]); //writes 108
Most of the network and filesystem APIs take and return buffers
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