Comments (7)
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.
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.
I tried concat-ing the buffer instead of converting it to a string and now it works as expected, thank you! :)
from iconv-lite.
Ok, but what if I get a string in ISO-8859-1 instead of base64?
from iconv-lite.
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.
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.
That seems reasonable. I'll try it monday morning as soon as I get to the office :)
from iconv-lite.
Related Issues (20)
- UCS-2BE, UCS-2LE not supported HOT 1
- Give users the option to load a subset of encodings to minimize js bundle size HOT 1
- Version w/Uint8Array option? HOT 2
- Problem with the type declaration of the first parameter of the decode method in index.d.ts? HOT 5
- Unable to resolve module string_decoder could not be found HOT 3
- Help bundling for vanilla web HOT 1
- Wrong maccyrillic decoding HOT 11
- can not work in cocos creator
- it affects page performance.
- setHighWaterMark
- Cant build project with iconv-lite HOT 1
- "Encoding not recognized: 'UNICODE UTF-8'" HOT 1
- Provide performant iconv.encodeInto API (previously iconv.byteLength) HOT 5
- UTF-16BE not exported correctly HOT 1
- when to update the version?
- Is the project still under maintenance? HOT 5
- How to stop iconv-lite from automatically console.log the entire document HOT 2
- CP737 encoding for Greek HOT 2
- Convert wrong character when utf8 to sjis
- to support in browser it's better modify the type declaration Buffer to Buffer | Uint8Array in index.d.ts HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from iconv-lite.