Binary xor

This is a discussion on Binary xor within the C++ Programming forums, part of the General Programming Boards category; I need to do something similar to this: string s = "pqr"; s[0] = (char)(s[0] ^ 0xF3); s[0] has value ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    15

    Binary xor

    I need to do something similar to this:

    string s = "pqr";
    s[0] = (char)(s[0] ^ 0xF3);

    s[0] has value 0x70 from the begining and I want the result to be 0x83 after running the code. But I get it to be 0xFFFFFF83 instead... Why's that? And more importantly, how do I get rid of it so I only get 0x83? Casting it into a char I think should force it down to one byte, but I can't get it to work...

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    0xFFFFFF83 is a 4-byte number - the char is probably being promoted to a larger data-type in the output routine. post the code you are using to print it with.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    15
    string s = "pqr";
    printf("%X %X %X \n",s[0],s[1],s[2]);
    s[0] = (char)(PWD[0] ^ 0xF3);
    printf("%X %X %X \n",s[0],s[1],s[2]);

    Like that!

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    the %X specifier expects a 4-byte variable to be passed on the stack...try something like this:

    Code:
    printf("%X %X %X \n", (unsigned int)((unsigned char)s[0]),(unsigned int)((unsigned char)s[1]),(unsigned int)((unsigned char)s[2]));
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    15
    Yes, that worked... But my main concern is that the string s will be in the right format, meaning only 0x83. And how come it printed it out correctly before I did the xor?

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    it probably has something to do with the fact that 0x83 interferes with the char's sign bit, causing the compiler to convert the extra bytes pushed onto the stack into twos-complement form (think it was supposed to be a negative number). the result was the corrupt data output.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    2

    Cast as unsigned char

    Quote Originally Posted by gaah
    Yes, that worked... But my main concern is that the string s will be in the right format, meaning only 0x83. And how come it printed it out correctly before I did the xor?
    Your char value is defaulted as signed char without the modifier, it is also treated as signed char on operation that most significant bits are set to 0Fh to denote a negative number. Try doing operations on unsigned char or cast as unsigned char when printing.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    15
    But I have it in a string. Can I have unsigned chars in a string?

  9. #9
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    yes.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You mean "no". If they're using an array of char, and not implicitly stating that it's unsigned, then no, they can't. Not guarinteed anyway. Some implementations may have char signed by default. In short, if you mean unsigned, then say so.

    [edit] Hm.. I see the origional post uses the string class. I was thinking in C mode. Nevermind. I'm not intimately familiar with the string class. [/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 10:07 AM
  2. Hex Chars to Binary (XOR Swap split)
    By fischerandom in forum C Programming
    Replies: 29
    Last Post: 11-26-2005, 06:13 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21