Reputation: 6323
I'm trying to port some code from Java to Node.js, and I've run into a little trouble.
Given the String "645553dd"
in Java, I can extract a series of bytes using .getBytes("ISO_8859_1")
that looks like { 54, 52, 53, 53, 53, 51, 100, 100 }
. However, I'm having a hard time doing the same in Node.js. I tried using buffers, converting to the similar ASCII charset, but no luck. I tried using node-iconv, but it kept throwing the error EILSEQ, Illegal character sequence
. How can I get the same set of bytes in Node.js?
Upvotes: 2
Views: 3259
Reputation: 123423
You should be able to convert from the Buffer's default encoding of UTF-8
to ISO-8859-1
with iconv
:
var Iconv = require('iconv').Iconv;
var ic8859 = new Iconv('UTF-8', 'ISO-8859-1');
console.log( ic8859.convert(new Buffer('645553dd')) );
Note that the values are output in base-16 -- 0x64 == 100
:
<SlowBuffer 36 34 35 35 35 33 64 64>
If you're still getting a EILSEQ
error, then your strings contains a character code that isn't supported by ISO-8859-1
. You'll either have to translate or ignore these characters:
var ic8859 = new Iconv('UTF-8', 'ISO-8859-1//IGNORE');
Or try a different encoding, like UTF-8:
console.log(new Buffer('645553dd'));
// <Buffer 36 34 35 35 35 33 64 64>
Upvotes: 4