char array[]   to   int HEX

Hi there, folks.
I'm kind of a newbie and I'm having problems making Arduinos talk with each other while using CRC to guarantee the validity of the received messages...
Basically, the MASTER sends to the SLAVE a message that begins with an !, followed by an Address Letter (eg: A) and the CRC for that letter (in hexadecimal).
eg: !A18

Here's some code:

if (startByte == '!') {  // if the Arduino has received a serial message that started with the character '!'
    unsigned long startTime = millis();  // set the startTime = to now
    int timeout = 1000;  // set the timout to 1 second
    while (millis() - startTime < timeout && Serial.available() < 3) {
      ;  // wait for the buffer to be filled with 3 characters while doing nothing
    }
    
    
    // Read the received Address and its CRC
    receivedAddressLetter[0] = Serial.read();
    for (int i=0; i < 2; i++) {
      receivedAddressCRC[i] = Serial.read();
    }

Now, I have the letter's CRC in an array, with each hexadecimal digit separated:

receivedAddressLetter == {'A'}
receivedAddressCRC == {'1', '8'}

How can I convert the receivedAddressCRC array into an hexadecimal int so that I can compare it with the CRC generated by the SLAVE Arduino???
Should I even convert it into an hexadecimal int??? Please look at the code below and give me some tips.

Thanks!

Here's some example code for the MASTER:

/*
 * 8-bit CRC Generator
 * by Dallas Temperature
 *
 * How it works:
 * Creates a CRC-8 based on the values of the data[] array
 * which, with further code, can then be appended to strings
 * or compared with received CRCs.
 */


byte data[1] = {'A'};

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.print('!');
  Serial.print('A');  // the Slave's address
  Serial.print( crc8(data, 1), HEX);
  delay(1000);
}


// The CRC-8 generator function
uint8_t crc8(uint8_t *addr, uint8_t len) {
  uint8_t crc = 0;
  
  while (len--) {
    uint8_t inbyte = *addr++;
    for (uint8_t i = 8; i; i--) {
      uint8_t mix = (crc ^ inbyte) & 0x01;
      crc >>= 1;
      if (mix) crc ^= 0x8C;
      inbyte >>= 1;
    }
  }
  return crc;
}

You could just send binary data instead of ASCII.

or to answer your original question (i always hate it when people answer a different question)
if its just 2nibbles then you could always do something like:

unsigned int crc;

crc = htoi(receivedAddressCRC[0])*16+htoi(receivedAddressCRC[1]);

with helper function:

//one hex digit in ascii to an int
int htoi (char c) {  //does not check that input is valid
    if (c<='9')
        return c-'0';
    if (c<='F')
        return c-'A'+10;
    if (c<='f')
        return c-'a'+10;
    return 0;
}

Thank you, mspguy, that works PERFECTLY!!! :slight_smile:

So, about sending the data as binary... now I'm curious. How could one achieve that and how would that be easier than this (provided one has the function you posted)???

To run with your example where '!' starts a message, then a letter is sent (e.g. 'A") then a CRC is sent (0x18hex)

you could send the serial data

unsigned char serialdata[3] = {'!','A',0x18};

Note that all a char really is is an 8 bit integral type (ie a number from -128 to 127 when signed and 0-255 when unsigned). So, when you write '!' or 'A' in a C/C++ program you're really writing 33 or 65 respectively. (see http://www.asciitable.com/ for a complete table)

You should note though that 0x18 (24 in decimal) on the ascii table is the CAN symbol, which is unprintable, so it makes viewing your serial data in a terminal impossible (or at least annoying).

The trade off is that you don't have to do any conversion:
so

unsigned int CRCdata = serialdata[2];

now.

Also, if you do this make sure you are sending 8 bit serial mode (as opposed to 7 bit serial mode) I can't seem to find any arduino specific documents that specified if the serial mode is 7 or 8 bit and the API doesn't allow it to be set :(. Maybe someone else here can fill in that detail.

Lastly you have to somehow "pack" data chunks that are larger than one byte (== one char).

EDIT: typo.