According to wikipedia, UCS-2 should always be big-endian , so it is strange that node only supports a bit of endian. You may consider the bug. However, the endian-ness switch is pretty straight forward as it is just a matter of byte order. So just swap the bytes around to go back and forth between the small and large end, for example:
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
source share