In Java, if we know the encoding for a byte array, we can decode it and get the corresponding characters as follows:
Charset charset = Charset.forName(encoding);
String decodedString = new String(byteArray, charset);
How can I achieve the same result in JavaScript?
Suppose I read a file that I know is encoded by Windows-1253 (Greek). To correctly display the contents of a file, I would have to decode the bytes in the file.
If we don’t decode (or do not open the file in a text editor that does not know the encoding), we can see something like this -
ÁõôÞ åßíáé ç åëëçíéêÞ.
But when this text (i.e. bytes) is decoded, we get
Αυτή είναι η ελληνική.
source
share