# SparkFun Forums

Where electronics enthusiasts find answers.

### How to convert ascii to HEX for a checksum

Your source for all things Atmel.

### How to convert ascii to HEX for a checksum#82696

By pcmofo
#82696
I am working on a RFID reader using the ID-20. It outputs serial data in the following format. Each [] is one ascii byte.

[STX]
[D1] [D2] [D3] [D4] [D5] [D6] [D7] [D8] [D9] [D10]
[CS1] [CS2]
[CR]
[LF]
[ETX]

The part I care about is the D1-D10, the actual data, 10 ascii charactors. CS1 and CS2 are 2 ascii charactors that are the checksum for the data. I would like to implement checking the data against the check sum but I cant seem to convert the charactors into the format I need. Here is how the data sheet says the checksum is done to the data

if the 10 digits of ascii come in and lets say they equal

2 4 0 0 C C 5 7 8 3

and the check sum is

3 C

then to get the checksum you need to take pairs of ascii and convert them into one HEX byte and then XOR the hex bytes. which would look like this.

2 4 0 0 C C 5 7 8 3
becomes
[24] [00] [CC] [57] [83]

and 3 C becomes [3C]

then you can do a simple [24] ^ [00] ^ [CC] ^ [57] ^ [83] (^ is XOR) to see if it = [3C]

The problem I am having is I dont know how to convert 10 ascii bytes into 5 pairs of 2 HEX bytes.

again... here is what I need to do (brackets [] used to illustrate seperate bytes)

ASCII [a] [c] [d] [e] [f]
HEX [ab] [cd] [ef]

Here is a piece of sample code that is not working that is attempting to do this

Code: Select all
``````int count = 0;
for ( int i = 0; i < 10; i+=2 )
{
hex[count] = data[i]+data [i+1];
count++;
}
``````
I found a link to someone who did this in assembly language but I am trying to do it on an arduino so the codes not even close. Anyone know how to do this?

### #82736

By Liencouer
#82736
your code has no xor, so far as i can see.
Code: Select all
``````uint16_t checksum;// the place where the checksum lives
uint16_t z* = &data[0]; //makes  u08[] data into a u16[]
uint8_t i = 10/2; // half as many u16s than there are u08 data points
do
{
--i;
checksum ^= z+i;
}while(i);``````
this should work-ish. it (should) pretend your u08[] is a u16[], and then you just xor along the u16 array. no idea if it compiles, but i think the function is closer to what you want than what you had

### #82738

By riden
#82738
You may find this Arduino ID-12 example helpful.

### #82869

By pcmofo
#82869
riden wrote:You may find this Arduino ID-12 example helpful.
Thank you! That is exactly what I needed! I am working on integrating the Checksum portion of that code into my existing EEPROM read/write and to make a complete RFID lock based off of the ID-12/ID-20 + arduino.