kabanos
kabanos

Reputation: 19

merging two array elements into one

After managing to correctly execute xor of chars between "$" and "*" I need to perform "IF statement" to check if the xor indeed equals "64". I need to merge these two chars and somehow compare to the xor. The problem is i variables type. XOR (sumcheck) is in hex, the b (merging of 6 and 4) is in dec. Should i convert XOR to dec value, or convert 64(dec) to 64(hex) value?

DEMO

#include <stdio.h>

int main(void) {

int i;
int xor = 0;
int b;
// $GPGLL,,,,,,V,N*64

char Received[18]= {'$','G','P','G','L','L',',',',',',',',',',',',','V',',','N','*','6','4'};
int loop;

// display array
//for(loop = 0; loop < 18; loop++)
//   printf("%c ", Received[loop]);

for(int i = 1; i<=14; i++)
    xor ^= Received[i];
    printf("%#02x  ", xor);
    printf("%d ", xor);

    b = ((Received[16]-'0') *10) + Received[17]-'0';
    printf("%d ", b);

if(xor == b){
    printf("WORKING!");
}
else{
    printf("not working");
}

return 0;
}

Upvotes: 0

Views: 83

Answers (1)

kiran Biradar
kiran Biradar

Reputation: 12732

You cannot pass a char to atoi because it expects its input to be pointer to char (char*).

If you wants to use atoi you need to form the string yourself and pass it to atoi as below.

 char a[3] = {Received[16],Received[17]};
 b = atoi(a); //Base 10

Use strol for base 16(HEX) as below

 b = strtol(a,NULL,16);

If you don't want to use atoi or strol you can do as below.

 b = ((Received[16]-'0') *10) + (Received[17]-'0'); //Base 10
 b = ((Received[16]-'0') *16) + (Received[17]-'0'); //Base 16

Upvotes: 1

Related Questions