最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reading a Windows-1252 file in Node JS - Stack Overflow

programmeradmin1浏览0评论

I'm using node to read a text document using readFile and within that document is a character

This is a windows-1252 character but it is being converted in javascript to utf-8 automatically. The correct character should actually display as Å.

Is there a way I can convert this character from utf-8 to windows-1252 to render the correct character?

The file is being read using nodes readFile method and is being read as utf-8, due to the lack of support for the necessary encoding.

fs.readFile(`${logDirectory}myText.txt`,"utf-8", (err, text) => { ... } 

I've tried a few options such as iconv-lite and legacy-decode but neither seem to return the correct result.

Any guidance appreciated.

I'm using node to read a text document using readFile and within that document is a character

This is a windows-1252 character but it is being converted in javascript to utf-8 automatically. The correct character should actually display as Å.

Is there a way I can convert this character from utf-8 to windows-1252 to render the correct character?

The file is being read using nodes readFile method and is being read as utf-8, due to the lack of support for the necessary encoding.

fs.readFile(`${logDirectory}myText.txt`,"utf-8", (err, text) => { ... } 

I've tried a few options such as iconv-lite and legacy-decode but neither seem to return the correct result.

Any guidance appreciated.

Share Improve this question asked Jun 24, 2020 at 14:52 Stephanie ParkerStephanie Parker 4011 gold badge8 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

You can try reading the file with the latin1-encoding as Windows-1252 is based on that:

fs.readFile(`${logDirectory}myText.txt`,'latin1', (err, text) => { ... } 

Also note that in NodeJS the utf-8 encoding is called utf8 instead of utf-8 as described here.

发布评论

评论列表(0)

  1. 暂无评论