Melab
Melab

Reputation: 2822

How do I convert a string in JavaScript to a sequence of bytes?

In Python 3, to convert a string to sequence of bytes, one uses String.encode(ENCODING) where ENCODING is the name of the encoding to use. If I have a character in my string that has a code point greater than 255, then it will still be converted to an array of bytes. This is useful if one needs to operate on the stringfor something like a demo of a cipher. The text can reconstructed by using ByteArray.decode(ENCODING).

I haven't seen anything similar for JavaScript. There is String.charCodeAt(), but this would convert a character like Ā to 256. That's not what I want.

Upvotes: 1

Views: 2179

Answers (3)

Tom Blodget
Tom Blodget

Reputation: 20782

"operate on the string for … a cipher": Probably not.

Ciphers are mathematical transformations of byte arrays. The result of encryption is not text so it can't be directly stored in a string.

A JavaScript string is a counted sequence of UTF-16 code units. (Also applies to VB4/5/6, VB, VBA, VBScript, C#, Java….) UTF-16 is one of several encoding of the Unicode character set. UTF-8 is another. Neither encodes to/decodes from arbitrary binary data.

You mentioned String.charCodeAt(). This just gives you one of the UTF-16 code units from the string.

Common ways of carrying and displaying binary data in strings are Base64 and hexadecimal. It's a bit weightier that way—and sender and receiver have to agree on both the character encoding of the string and the binary-to-text transformation—but many systems would rather pass text than binary.

Upvotes: 0

Derek 朕會功夫
Derek 朕會功夫

Reputation: 94319

You can read the bytes directly with the standard FileReader:

var str = "Āabc";
var b = new Blob([str], {type:"text/plain"});
var f = new FileReader();
f.addEventListener("loadend", function(){
    console.log(new Uint8Array(f.result));    // [196, 128, 97, 98, 99]
});
f.readAsArrayBuffer(b);

Upvotes: 0

guest271314
guest271314

Reputation: 1

You can use TextEncoder() .encode() and TextDecoder() .decode() methods

let decoder = new TextDecoder(/* character encoding */);
let encoder = new TextEncoder();

let encoded = encoder.encode(str);
let decoded = decoder.decode(encoded);

Upvotes: 1

Related Questions