I'm working with Node.JS. Node's buffers support little-endian UCS-2, but not big-endian, which I need. How would I do so?
Asked
Active
Viewed 3,209 times
1 Answers
5
According to wikipedia, UCS-2 should always be big-endian so it's odd that node only supports little endian. You might consider filing a bug. That said, switching endian-ness is fairly straight-forward since it's just a matter of byte order. So just swap bytes around to go back and forth between little and big endian, like so:
function swapBytes(buffer) {
var l = buffer.length;
if (l & 0x01) {
throw new Error('Buffer length must be even');
}
for (var i = 0; i < l; i += 2) {
var a = buffer[i];
buffer[i] = buffer[i+1];
buffer[i+1] = a;
}
return buffer;
}

broofa
- 37,461
- 11
- 73
- 73
-
That's about what I ended up doing. I'll file a bug report. – skeggse Sep 18 '11 at 17:26
-
3Or not...apparently they don't like that. https://github.com/joyent/node/issues/1684 – skeggse Sep 18 '11 at 17:27