Coder Social home page Coder Social logo

Comments (7)

ashtuchkin avatar ashtuchkin commented on May 29, 2024 3

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");
        });

from iconv-lite.

ashtuchkin avatar ashtuchkin commented on May 29, 2024 2

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");

from iconv-lite.

alexandernst avatar alexandernst commented on May 29, 2024 1

I tried concat-ing the buffer instead of converting it to a string and now it works as expected, thank you! :)

from iconv-lite.

alexandernst avatar alexandernst commented on May 29, 2024

Ok, but what if I get a string in ISO-8859-1 instead of base64?

from iconv-lite.

ashtuchkin avatar ashtuchkin commented on May 29, 2024

It depends on how that string was converted from the actual bytes.

If you have a JS string which was UTF-8-decoded from a buffer of ISO-8859-1-encoded data, then the recovery is possible only if the original string was all ASCII. UTF-8 is (obviously) not compatible with ISO-8859-1 and will produce all sorts of weird chars and decoding errors otherwise.

In general, you should attempt to get original bytes (in Buffer-s), that way it's easier and more robust.
If its not possible, then the best you can do is to try do encode it back as UTF8, then decode as ISO-8859-1, but it'll surely lose some information.

str = iconv.decode(new Buffer(str), "ISO-8859-1");

from iconv-lite.

alexandernst avatar alexandernst commented on May 29, 2024

I'm getting the string from this library: https://github.com/mscdex/node-imap

I fetch a mail part with:

f.on("message", function(msg, seqno){

    msg.on("body", function(stream, info){
        var buffer = "";
        stream.on("data", function(chunk){
            buffer += chunk;
        });

        stream.once("end", function(){
            //convert buffer from whatever encoding has to utf8
        });

..........

I'll ask that there too :)

from iconv-lite.

alexandernst avatar alexandernst commented on May 29, 2024

That seems reasonable. I'll try it monday morning as soon as I get to the office :)

from iconv-lite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.