0

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];
        }

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
coder
  • 21
  • 1
  • 2
  • What have you tried so far? Where are you stuck? – Nico Haase Jun 24 '20 at 07:33
  • i tried the method given in that link. But as per the documentation i have it is showing in example ,the checksum value is 0219 and i am getting 100219 the extra 10 in checksum. – coder Jun 24 '20 at 07:48
  • Please add all attempts to the question by editing it – Nico Haase Jun 24 '20 at 08:00
  • please check this is my code i have passed the message to convert it into bytes named here is bytes then i retrive the Uint8Array from the bytes called here is byteArray. i created the UInt16Array checksum and added all the calculation to the checksum. – coder Jun 24 '20 at 08:07
  • Next question: with all that code, what have you tried to spot the problem and fix it? – Nico Haase Jun 24 '20 at 08:12
  • I have debug that in crome dev tools. I dont know whats the exact problem – coder Jun 24 '20 at 08:43

0 Answers0