Node.JS Big-Endian UCS-2

I work with Node.JS. Node buffers support the small continental UCS-2, but not the big-endian I need. How can I do it?

+5
source share
1 answer

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; } 
+5
source

All Articles