# calculate XOR checksum over multiple datatypes

Hi!

I've written a sketch which outputs a NMEA GPS-phrase every second. I need to calculate the XOR-checksum over all the transmitted bytes and add that as a 2-digit hexadecimal value in the end.

I've done this plenty of times with a character array, but now I'm constructing the output from a few different sources.

``````Serial.write(stream[0]);
Serial.write(0x32);
Serial.write(0x03);
Serial.write(0x02);
Serial.write(0x24);
Serial.print("PKLDS,,A,");
if(LatDegrees<10){Serial.print('0');}
Serial.print(LatDegrees);
if(dblLat<10){Serial.print('0');}
Serial.print(dblLat,4);
Serial.print(',');
Serial.print(NZ);
Serial.print(',');
if(LongDegrees<100){if(LongDegrees<10){Serial.print("00");} else { Serial.print("0");}}
Serial.print(LongDegrees);
if(dblLong<10){Serial.print('0');}
Serial.print(dblLong,4);
Serial.print(',');
Serial.print(EW);
Serial.print(",,,,,E00,100,");
Serial.print(VehicleID);
Serial.println(",80,00,*");
``````

As you can see, the output is constructed of fixed values (',' - '0' ,..), chars (NZ - EW which hold the letter N S E or W), integers (LongDegrees) and doubles (dblLat,..).

I know this is not an example of good programming, but until now it seemed to do the trick. Obviously it's going to be a pain in the ass to calculate the XOR over all the bytes (almost 70 of them), as all output is in ASCII.

I was wondering if there exists a function that I can use just like Serial.print(), that constructs a char array of all the bytes, just as they would have been printed to the serial port? That way I could just run the entire thing through the XOR procedure and print to the serial port directly out of the array.

Thanks a lot!

I was wondering if there exists a function that I can use just like Serial.print(), that constructs a char array of all the bytes, just as they would have been printed to the serial port?

sprintf() can. Actually, it doesn't construct the char array. You have to do that. What it does is populate the array.