char data to uint8_t for CRC

I’m trying to convert char data to uint8_t for CRC but cant find any examples for doing this.
i have the CRC converting working but i need to able to change the uint8_t data[10] = {0x05,0x10,0x01,0x40}; to different values…

uint8_t data[10] = {0x05,0x10,0x01,0x40}; // = 49h 
char data1[10] = {0x11,0x11,0x11,0x11}; // Input data
int result;
uint8_t crc;
uint8_t j, i;
int d1 = 5, d2 = 16, d3 = 1, d4 = 64;
void setup() 
{
 Serial.begin(9600);
 delay(100); 

 result = CRC8(data, 4);
 Serial.println(result, HEX); // output = 49

 sprintf(data1, "0x%.2x,0x%.2x,0x%.2x,0x%.2x", d1, d2, d3, d4);
 Serial.println(data1); // output = 0x05,0x10,0x01,0x40

 // convert char data1[10] to uint8_t data[10] ?????

 result = CRC8(data, 4);
 Serial.println(result, HEX); // output = 4F ? why did this change
}

void loop() 
{
 
}

uint8_t CRC8( uint8_t *addr, uint8_t len)
{
   crc = 0;
   for( i=0; i<len; i++ ) 
    {
      uint8_t inbyte = addr[i];
      crc = crc ^ inbyte;
      for(j = 0; j < 8; j++)
      {
        if(crc & 0x01)
          crc = (crc >> 1) ^ 0x8C;
        else
          crc >>= 1;
        }
      }
     return crc;
}
char data1[10] = {0x11,0x11,0x11,0x11}; // Input data

OK, so data1 can hold up to 9 characters.

sprintf(data1, "0x%.2x,0x%.2x,0x%.2x,0x%.2x", d1, d2, d3, d4);

How many characters will this sprintf try to write? More than data1 has room for?

ok i See it

char data1[10] = {0x11,0x11,0x11,0x11}; // Input data

i have changed to

char data1[20] = {0x11,0x11,0x11,0x11}; // Input data

now the output is=
49
0x05,0x10,0x01,0x40
49

so how can i move data1 into data ??

You can't because it's a couple of times as big. And having an array of ASCII characters isn't going to help you much for a checksum I think...

Or are you just trying to have "hex values"? Then note, there is no such thing. It's all about representation :wink:

is there a way to change the values 0x05,0x10,0x01,0x40 ?

uint8_t data[10] = {0x05,0x10,0x01,0x40}; // = 49h

Change to what?

data isn't a constant so you can just write to it's members.

data[0] = 11;
data[1] = 0x23;
data[2] = 0b111;

vmansmx5:
is there a way to change the values 0x05,0x10,0x01,0x40 ?

uint8_t data[10] = {0x05,0x10,0x01,0x40}; // = 49h

Overwrite them? I mean, what exactly are you trying to do?

yes Overwrite them with int values to HEX

I think he (she?) is trying to parse a string.

This worked

 data[0] = 0x05;
 data[1] = 0x10;
 data[2] = 0x01;
 data[3] = 0x40;

result = CRC8(data, 4);
 Serial.println(result, HEX); // output = 49

I think is is correct ??

uint8_t data[10] = {0x00,0x00,0x00,0x00}; // CRC = 49h 05,10,01,40
int d1 = 5, d2 = 16, d3 = 1, d4 = 64;

data[0] = d1;
 data[1] = d2;
 data[2] = d3;
 data[3] = d4;

 result = CRC8(data, 4);
 Serial.println(result, HEX); // output = 4F ? why did this change

Full code

uint8_t data[10] = {0x01,0x01,0x01,0x01}; // CRC = 49h 05,10,01,40
int result;
uint8_t crc;
uint8_t j, i;
int d1 = 5, d2 = 16, d3 = 1, d4 = 64;

void setup() 
{
 Serial.begin(9600);
 delay(100); 

 result = CRC8(data, 4);
 Serial.println(result, HEX); // output = BE

 data[0] = d1;
 data[1] = d2;
 data[2] = d3;
 data[3] = d4;

 result = CRC8(data, 4);
 Serial.println(result, HEX); // output = 49
}

void loop() 
{
 
}

uint8_t CRC8( uint8_t *addr, uint8_t len)
{
   crc = 0;
   for( i=0; i<len; i++ ) 
    {
      uint8_t inbyte = addr[i];
      crc = crc ^ inbyte;
      for(j = 0; j < 8; j++)
      {
        if(crc & 0x01)
          crc = (crc >> 1) ^ 0x8C;
        else
          crc >>= 1;
        }
      }
     return crc;
}

vmansmx5:
yes Overwrite them with int values to HEX

Like I said, there is no such thing as a hex value or a int value. In the end it's all stored as bit's. But as far as the compiler is concerned it can be all different representations. It's like language, 1, one, één, eins, un, uno etc it's all the same thing. And as long as you know what representation/language it's in and you/the compiler understands that you're fine. For the compiler there is NO difference between 64, 0x41, 0100, 0b1000001 and even 'A'.

So instead of doing

 data[0] = d1;
 data[1] = d2;
 data[2] = d3;
 data[3] = d4;

Why not do this?

uint8_t data[10] = {5, 16, 1, 64};

And why do you need a 10 byte array if you just put 4 bytes into it?

i have changed this to 4

uint8_t data[4] = {5, 16, 1, 64};

These values will change within the code.

int d1 = 5, d2 = 16, d3 = 1, d4 = 64;
data[0] = d1;
data[1] = d2;
data[2] = d3;
data[3] = d4;

d1, d2, d3 and d4 change in code? And data does not? Then why not drop the four poorly named variables with an array as well? Or if they change, why do you need two copies of each?

uint8_t data[4] = {0, 0, 0, 0}; // CRC = 49h 05,10,01,40
int result;
uint8_t crc;
uint8_t j, i;
int d1 = 5, d2 = 16, d3 = 1, d4 = 64; // DEC val

void setup() 
{
 Serial.begin(9600);
 delay(100); 

 data[0] = d1;
 data[1] = d2;
 data[2] = d3;
 data[3] = d4;

 result = CRC8(data, 4);
 Serial.println(result, HEX); // output = 49
int d1 = 5, d2 = 16, d3 = 1, d4 = 64; // DEC val

That's just YOU writing it as a decimal. Did you read reply #12?

The above snippet is the same as

uint8_t data[4] = {5, 16, 1, 64}; // CRC = 49h 05,10,01,40
int result;
uint8_t crc;
uint8_t j, i; 

void setup()
{
 Serial.begin(9600);
 delay(100);

 result = CRC8(data, 4);
 Serial.println(result, HEX); // output = 49

And don't just dump every variable in global... For example

uint8_t j, i;

Have NO right to be global...

Yes d1 d2 d3 d4 values will change in code when dip switch are read and sensors

But what makes them different from data?

And if they are different, why are they not an array?

PS You are getting under 50% of answering counter questions. That's not really how you get your answers :wink:

that's just my point i am trying to put the d1 d2 d3 d4 values into the data array to get the CRC value