The JavaScript method String.fromCharCode()
behaves equivalently to Python's unichar()
in the following sense:
print unichr(213) # prints Õ on the console
console.log(String.fromCharCode(213)); // prints Õ on the console as well
For my purposes, however, I need a JavaScript equivalent to the Python function chr()
. Is there such a JavaScript function or a way to make String.fromCharCode()
behave like chr()
?
That is, I need something in JavaScript that mimics
print chr(213) # prints � on the console
So turns out you just want to work with raw bytes in node.js, there's a module for that. If you are a real wizard, you can get this stuff to work with javascript strings alone but it's harder and far less efficient.
print chr(213) # prints � on the console
So this prints a raw byte (
0xD5
), that is interpreted in UTF-8 (most likely) which is not valid UTF-8 byte sequence and thus is displayed as the replacement character (�).The interpretation as UTF-8 is not relevant here, you most likely just want raw bytes.
To create raw bytes in javascript you could use
UInt8Array
.You could optionally then interpret the raw bytes as utf-8:
What you are most likely trying to do has nothing to do with trying to interpret the bytes as utf8 though.
Another example:
In python 2.7 (Ubuntu):