Thread: XOR for a string

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    30

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I do not understand what you are trying to do. What does it mean to XOR two strings?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    30
    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....
    Last edited by pshirishreddy; 11-12-2008 at 10:53 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Did you check the return value of atoi() to make sure that it doesn't return an error and set errno.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    30
    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. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    30
    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. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    30
    than could you suggest me any solution for the same !!

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    30
    okie then will try out some thing from it..
    thank you laser light

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    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. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    30
    i would be implementing CRC then..

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    30
    NUCLEON
    that was very handy thanks a lot..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM