最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Convert ANSI file text into UTF8 in node.js usinf Fyle System - Stack Overflow

matteradmin3PV0评论

I´m trying to convert the text from an ANSI encoded file to an UTF8 encoded text in node.js.

I´m reading the info from the file using node´s core Fyle System. Is there any way to 'tell' to readFile that the encoding is ANSI?

fs = require('fs');
        fs.readFile('..\\\\LogSSH\\' + fileName + '.log', 'utf8', function (err, data) {
            if (err) {
                console.log(err);
            }

If not, how can I convert that text?

I´m trying to convert the text from an ANSI encoded file to an UTF8 encoded text in node.js.

I´m reading the info from the file using node´s core Fyle System. Is there any way to 'tell' to readFile that the encoding is ANSI?

fs = require('fs');
        fs.readFile('..\\\\LogSSH\\' + fileName + '.log', 'utf8', function (err, data) {
            if (err) {
                console.log(err);
            }

If not, how can I convert that text?

Share Improve this question asked Dec 13, 2016 at 15:35 Asier PomposoAsier Pomposo 6194 gold badges10 silver badges20 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Of course, ANSI is not actually an encoding. But no matter what exact encoding we're talking about I can't see any Microsoft code pages included in the relatively short list documented at Buffers and Character Encodings:

  • ascii - for 7-bit ASCII data only. This encoding is fast and will strip the high bit if set.

  • utf8 - Multibyte encoded Unicode characters. Many web pages and other document formats use UTF-8.

  • utf16le - 2 or 4 bytes, little-endian encoded Unicode characters. Surrogate pairs (U+10000 to U+10FFFF) are supported.

  • ucs2 - Alias of 'utf16le'.

  • base64 - Base64 encoding. When creating a Buffer from a string, this encoding will also correctly accept "URL and Filename Safe Alphabet" as specified in RFC4648, Section 5.

  • latin1 - A way of encoding the Buffer into a one-byte encoded string (as defined by the IANA in RFC1345, page 63, to be the Latin-1 supplement block and C0/C1 control codes).

  • binary - Alias for 'latin1'.

  • hex - Encode each byte as two hexadecimal characters.

If you work in Western Europe you may be tempted to use latin1 as synonym for Windows-1252 but it'll render incorrect results as soon as you print a symbol.

So the answer is no, you need to install a third-party package like iconv-lite.

In my case the convertion between types was due to the need to use special latin characters as 'í' or 'ó'. I solve it changing the encoding from 'utf8' to binary in the fs.readFile() function:

 fs.readFile('..\\LogSSH\\' + fileName + '.log', {encoding: "binary"}, function (err, data) {
            if (err) {
                console.log(err);
            }
Post a comment

comment list (0)

  1. No comments so far