i want to calculate the 16 bit checksum value in big endian format for bluetooth data transmission. i have passed the data to send is 123456 as per documentation. and got the checksum 100219 but in the documentation the checksum value is 0219. how to calculate same like that. Calculating a 16 bit checksum?. i am following this link but not get output as expected as implemented in javascript
// ASCII only
function stringToBytes(string) {
var array = new Uint8Array(string.length);
for (var i = 0, l = string.length; i < l; i++) {
array[i] = string.charCodeAt(i);
}
return array.buffer;
}
datalength = dec2hex(msg.length);
megPassedToChecksum = 'ABAB'+ datalength + msg ;
var bytes = stringToBytes(megPassedToChecksum);
var byteArray = new Uint8Array(bytes);
var checksum = new Uint16Array();
checksum = 0;
val = [];
length = byteArray.length;
var even_length = length - (length % 2); // Round down to multiple of 2
for (var i = 0; i < even_length; i += 2) {
var val = byteArray[i] + 256 * byteArray[i + 1];
checksum += val;
}
if (i < length) {
checksum += byteArray[i];
}