-
Notifications
You must be signed in to change notification settings - Fork 28.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't convert Buffer into String correctly with binary encoding #12908
Comments
That is the output that you get, though: The string
Yes, that’s ÊÅ^N encoded as ISO-8859-1 (which is known as
Yes. You’re using So, what’s going on is that Node takes your Buffer, decodes to a string it as if it were ISO-8859-1-encoded text (because you said so), then re-encodes that string into a Buffer as UTF-8 (because it has to encode it somehow to write it to stdout, and UTF-8 is the most sensible choice), and then sends that Buffer to the terminal (where it gets decoded as UTF-8 again, in all likelihood.)
Let me know if this doesn’t help! |
Thank you so much for the useful and complete explanation, you saved my day! |
Okay, I’m closing this as an answered question then – feel free to ask any follow-up questions, here or on https://github.com/nodejs/help. :) |
Hi there! Sorry but I'm still a bit confused. In the nodejs docs I see this example: const buf1 = Buffer.from('this is a tést');
const buf2 = Buffer.from('7468697320697320612074c3a97374', 'hex');
console.log(buf1.toString());
// Prints: this is a tést
console.log(buf2.toString());
// Prints: this is a tést Why can't I interact with a binary representation the same way I can with hex? For instance: const buf3 = Buffer.from('11101...0100', 'binary') // '...' just used to truncate
buf3.toString()
// Prints '11101...0100', not 'this is a tést' (which I want!) Furthermore: buf1.toString('hex')
// Prints: 7468697320697320612074c3a97374
buf1.toString('binary')
// Prints: 'this is a tést', not '11101...0100' (which I want!) Thank you very much for your help. |
@dustinmichels |
I see. Thank you. |
Hello, I was working with binary files and Buffer, and encountered this problem.
Code:
I would expect as output something like:
which is exactly the 3 bytes 0xca, 0xc5, 0x0e
Instead of this I got the equivalent of:
Is it the expected behavior? If so, how can I obtain the string "\xca\xc5\x0e" from the Buffer?
The text was updated successfully, but these errors were encountered: