Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Ascii Encoding

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

like image 422
Deepak Banka Avatar asked Sep 02 '26 11:09

Deepak Banka


1 Answers

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

like image 189
Iain Ballard Avatar answered Sep 05 '26 01:09

Iain Ballard