More stuff to throw into the mix whilst we figure out what is required.
Code:
#include <stdio.h>
#include <string.h>
// format of line is as follows
// :llaaaaaaddddddddss
// :0400400086850500AC
// : is a colon
// ll is a byte indicating the number of dd data bytes
// aaaaaa is a 3 byte address in LSB order
// dd are the data bytes
// ss is the checksum of all the bytes which precede it
// ( ll+aa+dd+ss ) % 256 = 0
void decode_line ( char *line ) {
int i;
int sum = 0;
printf( "Decoding %s", line );
// skip the :, and the \n at the end of the line
for ( i = 1 ; i < strlen(line)-1; i += 2 ) {
int byte;
sscanf( &line[i], "%2x", &byte );
printf( "byte=%02x\n", byte );
sum += byte;
}
printf( "Sum=%x\n", sum );
printf( "Sum modulo 256=%x\n", sum % 256 );
}
int main ( ) {
// example line, as you would get using fgets
decode_line( ":0400400086850500AC\n" );
return 0;
}