Skip to content

Instantly share code, notes, and snippets.

@camilloaddis
Created April 1, 2020 23:53
Show Gist options
  • Save camilloaddis/ae8aa51ffe7a5694267b085695d8f2f3 to your computer and use it in GitHub Desktop.
Save camilloaddis/ae8aa51ffe7a5694267b085695d8f2f3 to your computer and use it in GitHub Desktop.
A simple javascript checksum calculator for HITACHI AC Remote: R-LT0541-HTA/Y.K.1.1-1 V2.3
function hex2bin(hex){
return ("00000000" + (parseInt(hex, 16)).toString(2)).substr(-8);
}
function checksum(bytes){
bytes = bytes.match(/.{1,2}/g);
let removed = bytes.splice(0,5);
var s = 0;
bytes.forEach(function(hexByte){
hex2bin(hexByte).match(/.{1,4}/g).forEach(function(halfByte){
s+= parseInt(halfByte.split('').reverse().join(''),2)
})
})
bytes.push(parseInt(hex2bin(s.toString(16)).split('').reverse().join(''), 2).toString(16).toUpperCase());
return removed.join('') + bytes.join('');
}
console.log(checksum("B2AE4D91F061C80000000010"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment