使用Javascript的8位XOR校验和 [英] 8-bit XOR checksum using Javascript
问题描述
我试图模仿一个windows应用程序,格式化消息并通过USB将它发送到通过USB发送给显示该消息的设备。
应用程序会计算一个校验和,并粘贴消息后面的内容,否则设备将不会接受该命令。校验和不是crc8校验和,但它是什么,然后呢?
使用USB监视器,我已经看到以下测试用例:
ASCII:< L1>< PA>< IB>
HEX: 3c4c313e3c50413e3c49423e3c4d413e3c57433e3c4f4d3e5465737421
校验码: 6A
ASCII:< L1>< PA>< IB>< MA>< WC> OM> Testa!
HEX: 3c4c313e3c50413e3c49423e3c4d413e3c57433e3c4f4d3e546573746121
校验和: 0B
在这种情况下,可以使用下面的表达式来进一步测试:
ASCII:< L1>< PA>< IB>< code>
HEX: 3c4c313e3c50413e3c49423e3c4d413e3c57433e3c4f4d3e5465737420736f6d65206d6f7265
校验码: 4A
ASCII:< L1>< PA>< IE>< WC>< ]
HEX: 3c4c313e3c50413e3c49453e3c4d413e3 c57433e3c4f453e20
校验和: 52
< a href =https://www.scadacore.com/tools/programming-calculators/online-checksum-calculator/ =nofollow noreferrer>本网站返回正确的校验和第一行(CheckSum8 Xor)。我试图模仿这种功能。 (注意:当您发送ASCII值时,网站会因为它包含<>
字符而吓坏了,请使用HEX值!)
目前,我的代码是这样的:
let hex = ascii2hex('< L1>< PA>< IB>< MA>< WC> OM> Test!')//或其中一个ascii值
让checksum = chk8xor(hex)
console.log校验和)
函数ascii2hex(str){
var arr = []; (var i = 0,l = str.length; i <1; i ++){
var hex = Number(str.charCodeAt(i))。toString(16);
。
arr.push(十六进制);
}
return arr;
函数chk8xor(byteArray){
let checksum = 0x00
for(let i = 0; i< byteArray.length - 1; i ++)
校验和^ = byteArray [i]
返回校验和
}
(javascript)
代码有效,但返回的校验和不正确。例如,第一个测试用例返回 50
而不是 6A
。
我确信我错过了一些简单的事情。
我在做什么与上面提到的网站有所不同?
这里不需要任何十六进制字符串,只需取ASCII值并与校验和进行异或运算即可:
< OM>< WC>< IE><>< WC>< OE> ',] for(let str test){let cs = 0; for(let char str)cs ^ = char.charCodeAt(0)console.log(str,cs.toString(16))}
$ p
$ p $如果您喜欢功能风格,您可以将内部循环写为
let cs = [... str] .map(a => a.charCodeAt(0))。
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 command. 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?
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);
这篇关于使用Javascript的8位XOR校验和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!