# Thread: XOR for a string

1. ## XOR for a string

well my problem here was to check for some error by using the bitwise XOR (^) operator on two strings.
so for doing that i need to convert the strings to binary values i used the below method but all in vain
Code:
```  main(){
char *msg;
int a;
msg="hi how are you ";
a=atoi(msg);
printf("%d",a);
}```
the output i get is '0' simply zero.. i dont understand whatz the problem. please help me out..

2. I do not understand what you are trying to do. What does it mean to XOR two strings?

3. its like checking the data here is the simple example
of xor of two int values
Code:
```int a=10; //      1010
int b=5;  //       0101
c=a^b;   //       1111 = 15;```
the same thing i have to do it with strings any how the problem is how to convert the strings in to binary values as in like characters can be done using atoi funciton .

let me know if i am still not clear....

4. Sorry, I still do not understand what you are trying to do. Yes, clearly you have some sort of checksum algorithm in mind, but just talking about XOR is too vague. Even in your integer example, what exactly is the significance of a, b and c, how will you go about checking for errors? If you can establish that idea, then perhaps you can think about how to apply it for strings.

5. Did you check the return value of atoi() to make sure that it doesn't return an error and set errno.

6. yes it is indeed related to a checksum algorithm..
that is not actually the point i am worried about
its about the how to get binary values for a string
to perform such an operation..

7. Originally Posted by pshirishreddy
yes it is indeed related to a checksum algorithm..
that is not actually the point i am worried about
its about the how to get binary values for a string
to perform such an operation..
It seems to me that that is precisely the problem. How you "get binary values for a string" depends on what is your checksum algorithm, since it is the input for that algorithm. With no clearly defined algorith, the type of input is not clearly defined.

8. Code:
```#include <stdio.h>

main(){
char msg[32];
long int a;
gets(msg);
printf("&#37;s",msg);
a=atoi(msg);
printf("%d",a);
}```
this is the total code and i get the value of a as 0.

9. That is because atoi() expects a numeric string and returns 0 if the first character is non-numeric, but your input is pretty much any string.

10. than could you suggest me any solution for the same !!

11. Originally Posted by pshirishreddy
than could you suggest me any solution for the same !!
Without knowing your algorithm, not really. I could make some suggestions, of course, but they will all be blind stabs in the dark. For example, I could suggest that you take a hash of the string. Or I could suggest that you sum the values of the characters. But maybe these suggestions are as bad as just taking the value of the first character of the string... who knows?

12. okie then will try out some thing from it..
thank you laser light

13. You don't need to convert to binary since everything in a computer is already in binary. To "XOR two strings" may mean this:
Code:
```char s1[] = "abcdefg";
char s2[] = "hijklmn";
int i;
for (i = 0; s1[i]; ++i)
s1[i] ^= s2[i];```
Note that (for this implementation) both strings must be the same length and s1 is destroyed (contains the result). You may want to change that.

14. i would be implementing CRC then..

15. NUCLEON
that was very handy thanks a lot..