hash - 8-bit XOR checksum using Javascript - Stack Overflow

I'm trying to mimic a windows application that formats a message and sends it over UART via USB to

I'm trying to mimic a windows application that formats a message and sends it over UART via USB to a device that shows that message.

The application calculates a checksum and pastes that after the message, otherwise the device will not accept the mand. The checksum is NOT a crc8 checksum, but what is it, then?

Using a USB monitor, I've seen the following test cases:

ASCII: <L1><PA><IB><MA><WC><OM>Test!
HEX: 3c4c313e3c50413e3c49423e3c4d413e3c57433e3c4f4d3e5465737421
Checksum: 6A

ASCII: <L1><PA><IB><MA><WC><OM>Testa!
HEX: 3c4c313e3c50413e3c49423e3c4d413e3c57433e3c4f4d3e546573746121
Checksum: 0B

ASCII: <L1><PA><IB><MA><WC><OM>Test some more
HEX: 3c4c313e3c50413e3c49423e3c4d413e3c57433e3c4f4d3e5465737420736f6d65206d6f7265
Checksum: 4A

ASCII: <L1><PA><IE><MA><WC><OE>[SPACE]
HEX: 3c4c313e3c50413e3c49453e3c4d413e3c57433e3c4f453e20
Checksum: 52

This website returns the correct checksum in the first row (CheckSum8 Xor). I'm trying to mimic that functionality. (Note: the website freaks out when you send the ASCII values because it contains <> characters. Use the HEX values instead!)

Currently, my code does this:

let hex = ascii2hex('<L1><PA><IB><MA><WC><OM>Test!') // or one of the other ascii values
let checksum = chk8xor(hex)
console.log(checksum)

function ascii2hex(str) {
  var arr = [];
  for (var i = 0, l = str.length; i < l; i ++) {
    var hex = Number(str.charCodeAt(i)).toString(16);
    arr.push(hex);
  }
  return arr;
}

function chk8xor(byteArray) {
  let checksum = 0x00
  for(let i = 0; i < byteArray.length - 1; i++)
    checksum ^= byteArray[i]
  return checksum
}

(javascript)

The code works, but returns an incorrect checksum. For instance, the first test case returns 50 instead of 6A.

I'm sure I'm missing something simple.
What am I doing differently than the website I mentioned above?

I'm trying to mimic a windows application that formats a message and sends it over UART via USB to a device that shows that message.

The application calculates a checksum and pastes that after the message, otherwise the device will not accept the mand. The checksum is NOT a crc8 checksum, but what is it, then?

Using a USB monitor, I've seen the following test cases:

ASCII: <L1><PA><IB><MA><WC><OM>Test!
HEX: 3c4c313e3c50413e3c49423e3c4d413e3c57433e3c4f4d3e5465737421
Checksum: 6A

ASCII: <L1><PA><IB><MA><WC><OM>Testa!
HEX: 3c4c313e3c50413e3c49423e3c4d413e3c57433e3c4f4d3e546573746121
Checksum: 0B

ASCII: <L1><PA><IB><MA><WC><OM>Test some more
HEX: 3c4c313e3c50413e3c49423e3c4d413e3c57433e3c4f4d3e5465737420736f6d65206d6f7265
Checksum: 4A

ASCII: <L1><PA><IE><MA><WC><OE>[SPACE]
HEX: 3c4c313e3c50413e3c49453e3c4d413e3c57433e3c4f453e20
Checksum: 52

This website returns the correct checksum in the first row (CheckSum8 Xor). I'm trying to mimic that functionality. (Note: the website freaks out when you send the ASCII values because it contains <> characters. Use the HEX values instead!)

Currently, my code does this:

let hex = ascii2hex('<L1><PA><IB><MA><WC><OM>Test!') // or one of the other ascii values
let checksum = chk8xor(hex)
console.log(checksum)

function ascii2hex(str) {
  var arr = [];
  for (var i = 0, l = str.length; i < l; i ++) {
    var hex = Number(str.charCodeAt(i)).toString(16);
    arr.push(hex);
  }
  return arr;
}

function chk8xor(byteArray) {
  let checksum = 0x00
  for(let i = 0; i < byteArray.length - 1; i++)
    checksum ^= byteArray[i]
  return checksum
}

(javascript)

The code works, but returns an incorrect checksum. For instance, the first test case returns 50 instead of 6A.

I'm sure I'm missing something simple.
What am I doing differently than the website I mentioned above?

Share Improve this question edited Jan 18, 2018 at 19:13 Jeff Huijsmans asked Jan 18, 2018 at 19:04 Jeff HuijsmansJeff Huijsmans 1,4181 gold badge16 silver badges38 bronze badges 1
  • shouldn't it be i < byteArray.length instead of i < byteArray.length - 1 ??? – iKK Commented Nov 9, 2022 at 11:27
Add a ment  | 

1 Answer 1

Reset to default 5

You don't need any "hex" strings here, just take the ASCII value and XOR it with the checksum:

test = [
    '<L1><PA><IB><MA><WC><OM>Test!',
    '<L1><PA><IB><MA><WC><OM>Testa!',
    '<L1><PA><IB><MA><WC><OM>Test some more',
    '<L1><PA><IE><MA><WC><OE> ',
]


for (let str of test) {
    let cs = 0;
    for (let char of str)
        cs ^= char.charCodeAt(0)
    console.log(str, cs.toString(16))
}

If you like the "functional" style, you can write the inner loop as

let cs = [...str].map(a => a.charCodeAt(0)).reduce((a, b) => a ^ b);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745225712a4617452.html

相关推荐

  • hash - 8-bit XOR checksum using Javascript - Stack Overflow

    I'm trying to mimic a windows application that formats a message and sends it over UART via USB to

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信