1

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.

Melab
  • 2,594
  • 7
  • 30
  • 51

3 Answers3

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);
guest271314
  • 1
  • 15
  • 104
  • 177
0

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);
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
0

"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.

Tom Blodget
  • 20,260
  • 3
  • 39
  • 72
  • I know that the cryptographic operations are not performed directly on strings. That's why I'm trying to convert between strings and byte arrays. And the result can certainly be stored as a string in Python 3 by using Latin-1 encoding. – Melab Nov 16 '17 at 17:30
  • Okay but JavaScript strings are UTF-16. So, do you want to convert the bytes as if they where Latin1 and store them in a string? – Tom Blodget Nov 16 '17 at 17:38