Using XOR to create a checksum

I was instructed to develop some meager instructions for developing a control byte from a sequence of bytes between the cash register and the scale, and then implement it in C.

Instructions from the manual (to get a checksum):

ID XOR B1 XOR B2 XOR B3 XOR B4 XOR B5

Other similar protocols mention that this may be the XORingmost significant bit in each byte - (I don’t know how to do this or even if necessary)

Fortunately, I have a scale and a computer to check exactly what is sent, ID = asciiand I am B1 to B5the weight, in this example, when the weight on the scale shows 00748; The sequence returned in ascii isi00748, then the checksum is sent:45 decimal.

Some code I tried below unfortunately returns 82 in decimal format.

    char out = 0;
    char in[6] = "i00748";
    int i;

    for(i=0; i<6; i++)
        out = out ^ in[i];

    printf("%d",out);

Any help was appreciated.

+4
source share
1 answer

The protocol uses 7-bit ASCII with odd parity for each bit. This means that the MSB of each byte (including the control byte) is 0. And each bit in the check byte is 0 if there is an odd number 1 in this position, otherwise.

To illustrate, here is the message shown in binary

01101001   i   ID
00110000   0   B1
00110000   0   B2
00110111   7   B3
00110100   4   B4
00111000   8   B5
--------
00101101   -   check byte

, , 1, 1, , 0. MSB, 0 .

, , , ,

out = 0x7f;
+4

Source: https://habr.com/ru/post/1607387/


All Articles