2

I have a file.txt and content of it is:

\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064\u0021

It is 'Hello World!' in UTF-16.

If my code is:

let data = '\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064\u0021';
console.log(data);

It is print out: 'Hello World!'

But If I use readFileSync() and console.log(), it isn't print out 'Hello World!':

let data = fs.readFileSync('file.txt',{encoding:'utf8', flag:'r'});
console.log(data);

Why?

Example:

Example

3
  • 3
    What about changing the encoding string to be utf16? Commented Jun 21, 2021 at 13:19
  • 2
    You have a utf16 file you read with utf8 encoding? Commented Jun 21, 2021 at 13:20
  • I mean: '\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064\u0021' in normal text file Commented Jun 21, 2021 at 13:47

1 Answer 1

4

In the source code of a JavaScript string literal, \u followed by four characters represents a position in the Basic Multilingual Plane.

The same numbers, expressed in bytes, mean the same thing in various Unicode encodings.

Your text file doesn't contain bytes that represent those characters.

It contains bytes which represent the \ character, the u character, etc.

Sign up to request clarification or add additional context in comments.

2 Comments

So, what should i do to convert data read from file to utf8?
I'd store the data as text (as opposed to JS source code) in the first place.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.