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.

1

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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);

2 Comments

I'm not using a file.
@Melab The FileReader in the code is reading a blob, not a file. Take a closer look at the code.
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.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.