There are many ways of writing the code. For example, I would change the last line to:I like the code here, but I don't understand why you checkSum &= 0x7F; /* Clamp to 7 bits */ before the one's complement. Is it necessary?
I've been using Derek's algorithm for both Roland and Yamaha machines, which is similar, but then I thought the following might be even simpler, but I'm no C expert?
In the Roland manuals they seem to offer a very convoluted and confusing explanation of the checksum algorithm.Code:
/*possible Yamaha sysex message data starts at byte 5*/ unsigned data[] = {0xf0,0x43,0x7f,0x7f,0x7f,0x01,0x00,0x00,0x29,0x05,0x00,0xf7}; unsigned dataLength=sizeof(data) / sizeof(data[0]); unsigned checkSum = 0; unsigned i; for (i = 5; i < dataLength-2; ++i) { /* Read/write data from/to packet */ checkSum += data[i]; /* Incorporate byte into sum using array index */ } *data = 128 - checkSum & 127; /* Write the check sum after the packet */
Code:
*data = (-checkSum) & 0x7f;
Statistics: Posted by mx49 — Mon Apr 22, 2024 6:05 pm