Sto lavorando con Node.JS. Node's buffers supporto little-endian UCS-2, ma non big-endian, di cui ho bisogno. Come lo farei?Node.JS Big-Endian UCS-2
5
A
risposta
5
Secondo wikipedia, UCS-2 should always be big-endian quindi è strano che il nodo supporti solo little endian. Potresti considerare di sporgere un bug. Detto questo, il passaggio da endianità è abbastanza semplice poiché si tratta solo di un byte order. Quindi basta scambiare byte per andare avanti e indietro tra little e big endian, in questo modo:
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;
}
Questo è quello che ho finito per fare. Presenterò un bug report. – skeggse
Oppure no ... a quanto pare non gli piace. https://github.com/joyent/node/issues/1684 – skeggse