I
I
Ivan_korolev2015-07-19 17:05:03
JavaScript
Ivan_korolev, 2015-07-19 17:05:03

How to swap bytes?

This self-made function calculates CRC-CCITT (Kermit) and everything would be fine, but it returns
FB56 instead of 56FB... what is the best way to swap bytes?

function crc16(data) {
  var crc = 0; 
  for (var i = 0; i < data.length; i++) {
    crc ^= data.charCodeAt(i);
    for (var j = 0; j < 8; j++) {
      if (crc & 1) crc = 0x8408 ^ crc >>> 1;
      else crc >>>= 1;			
    }
  } 
  return crc;
}


console.log(crc16("test").toString(16));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur Shemsedinov, 2015-07-19
@Ivan_korolev

return ((crc & 0xFF) << 8) + ((crc >> 8) & 0xFF);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question