0

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;
}
4
  • 1
    Not the cause of your problem, but this is wrong: printf("%#02x ", xor, "%d ", xor); it should be printf("%#02x %d\n", xor, xor); Commented Sep 26, 2018 at 9:22
  • I am missing the bracket around the for-loop Commented Sep 26, 2018 at 9:42
  • 1
    Please don't "correct" the code in your question as the answer bewlow doesn't make sense anymore. If you have another question, ask another question. Commented Sep 26, 2018 at 9:44
  • sorry for that stupid mistake Commented Sep 26, 2018 at 9:45

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

atoi did work, however i wasn't able to display it. Second option is easier. Also i still have problems with the variables.. DEMO Am i right? i need to convert hex (xor sumcheck) value to dec?
@kabanos I'm not able to access your Link. What problems you are having with variables?
i've updated the question, your answer was very helpful. however now i need to perform if statement to check if xor (sumcheck) equals b (merging of 6 and 4). xor is in hex and b is in dec value. Can i convert 64(dec) to 64(hex)?
@kabanos use Base 16 conversion b = ((Received[16]-'0') *16) + Received[17]-'0'; Same I have already included in the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.