question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Convert from/to encodings with iconv-lite only

See original GitHub issue

Take this example:

mystr = new Buffer("base64 string with ISO-8859-1 encoding goes here", "base64").toString();

buffer = new Buffer(buffer, "ISO-8859-1"); <--- this will fail as Buffer doesn't support that encoding

buffer = iconv.decode(buffer, "ISO-8859-1");
buffer = iconv.encode(buffer, "utf8").toString("utf8");

This code should be able to convert from a ISO-8859-1 (or any other encoding) string to UTF8, but it will fail because of the second line.

Can that be done using iconv-lite?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
ashtuchkincommented, Oct 4, 2013

You’re inadvertently converting the chunk to string by doing buffer += chunk. You should keep it as a buffer, something like this:

        var buffer = new Buffer(0);
        stream.on("data", function(chunk) {
            buffer = Buffer.concat([buffer, chunk]);
        });

        stream.once("end", function(){
            str = iconv.decode(buffer, "ISO-8859-1");
        });

2reactions
ashtuchkincommented, Oct 4, 2013

Yes, sure. iconv.decode(buf, encoding) takes binary data and decodes it into a JS string. So, what you’re need here is:

originalData = new Buffer("base64 string with ISO-8859-1 encoding goes here", "base64");  // Notice, no .toString().
jsStr = iconv.decode(originalData, "ISO-8859-1");

// Here you can use jsStr as usual javascript String:
jsStr.replace("hello", "world");

// If you need a buffer with this string UTF-8 encoded, then you can use Buffer like that:
utf8EncodedStringBuf = new Buffer(jsStr);  // utf8 is the default encoding.

// Or, equivalent
utf8EncodedStringBuf = iconv.encode(jsStr, "utf8");
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use the iconv-lite.encodingExists function in ... - Snyk
Hack to make iconv load the encodings module, otherwise jest crashes. Compare // https://github.com/sidorares/node-mysql2/issues/489 require('iconv-lite').
Read more >
Use iconv to change character string encoding
First we will demonstrate converting the three common encoding (ASCII, CP1252, and ISO-8559-1) to/from UTF-8, then using iconv.convert() to convert between ...
Read more >
iconv-lite not decoding everything properly, even though I'm ...
Does C++ support converting between character encodings other than UTF-8, UTF-16, and UTF-32? 1 · html textarea using specific character set · 0....
Read more >
iconv-lite - npm
Convert character encodings in pure javascript.. Latest version: 0.6.3, last published: a year ago. Start using iconv-lite in your project ...
Read more >
iconv - Manual - PHP
iconv — Convert a string from one character encoding to another ... returns an empty string (or you'll get a notice but only...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found