Degree symbol issue in node js file write with utf-8 encoding

555 views Asked by At

I am using nodeJs npm fs module to read and write file using utf-8 encoding. File read and write operation done successfully. But when I open the file in windows wordpad the Degree Celcius (°C) symbol comes as (â°C). If I open the same file in VS code editor and notepad Degree Celcius(°C) symbol comes correctly, the issue is only with the windows wordpad application.

Here is file write code in node Js.

var fs = require('fs');    
let data="Temperature is 25°C";
fs.writeFile("./output.txt", data, 'utf8', function (err) {
        if (err) {
          console.error("Error while writing the output file");
        } else {
          console.log("File write operation done!");
        }
});
1

There are 1 answers

0
O. Jones On

It looks to me like word pad thinks your file is encoded in Latin1 (iso-8859-1) or Windows-1252. These character encodings use one byte per character, so it renders the utf8 unicode stuff wrong.

Best fix: use notepad or notepad++ and kiss wordpad goodbye.

Here's a way to tweak the file so various apps know it's unicode. https://superuser.com/questions/1275925/make-wordpad-properly-open-a-utf-8-or-utf-16-encoded-file