F. Santiago
F. Santiago

Reputation: 850

NodeJS Extended ASCII Support

I'm trying to read a file that contains extended ascii characters like 'á' or 'è', but NodeJS doesn't seem to recognize them.

I tried reading into:

Tried differente encoding types:

as referenced on http://nodejs.org/api/fs.html

Is there a way to make this work?

Upvotes: 3

Views: 5650

Answers (3)

F. Santiago
F. Santiago

Reputation: 850

The file I was trying to read was in ANSI encoding. When I tried to read it using the 'fs' module's functions, it couldn't perform the conversion of the extended ASCII characters.

I just figured out that nodepad++ is able to actually convert from some formats to UTF-8, instead of just flagging the file with UTF-8 encoding.

After converting it, I was able to read it just fine and apply all the operations I needed to the content.

Thank you for your answers!

Upvotes: 2

John Vilk
John Vilk

Reputation: 33

I realize this is an old post, but I found it in my personal search for a solution to this particular problem.

I have written a module that provides Extended ASCII decoding and encoding support to/from Node Buffers. You can see the source code here. It is a part of my implementation of Buffer in the browser for an in-browser filesystem I've created called BrowserFS, but it can be used 100% independently of any of that within NodeJS (or Browserify) as it has no dependencies.

Just add bfs-buffer to your dependencies and do the following:

var ExtendedASCII = require('bfs-buffer/js/extended_ascii').default;
// Decodes the input buffer as an extended ASCII string.
ExtendedASCII.byte2str(aBufferWithText);
// Encodes the input string as an extended ASCII string.
ExtendedASCII.str2byte("Some text");

Alternatively, just adapt the module into your project if you don't want to add an extra dependency to your project. It's MIT Licensed.

I hope this helps anyone in the future who finds this page in their searches like I did. :)

Upvotes: 1

seth
seth

Reputation: 345

I use the binary type to read such files. For example

var fs = require('fs');

// this comment has I'm trying to read a file that contains extended ascii characters like 'á' or 'è',

fs.readFile("foo.js", "binary", function zz2(err, file) {  
    console.log(file);
});

When I do save the above into foo.js, then the following is shown on the output:

var fs = require('fs');

// this comment has I'm trying to read a file that contains extended ascii characters like '⟡ 漀爀 ✀',

fs.readFile("foo.js", "binary", function zz2(err, file) {  
    console.log(file);
});

The wierdness above is because I have run it in an emacs buffer.

Upvotes: 4

Related Questions