Thread: newbie needs help with xor.

  1. #1
    Unregistered
    Guest

    newbie needs help with xor.

    I've just begun to program in C++ and can write simple things, like calculators and stuff like that. So anyway xor looks pretty simple and I have been willing to spend days locked in my room reading and dumping out code but it never works because, god never looks after the noobs. PLEASE HELP I use DJGPP, and would be most appreciative if you could help out. Sorry if there are any grammar and spelling errors I also won’t be able to reply for a while, thanks to all who help.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Perhaps you should describe your problem instead of just complaining about your program not working. Any number of things could be happening to make your use of XOR not work, so it's best to break the program down into the smallest possible code that still shows the problem and then post it with a description of what it is doing and what you want it to do. For example:
    -----------------------------------
    "My program doesn't seem to work right, I want to take a number and zero out the value by having the variable use XOR on itself, but every time I try to compile, I get two errors. Here is the code:"
    Code:
    #include <iostream>
    
    int main()
    {
      float test = 1234;
      std::cout<< (test ^ test) <<'\n';
      return 0;
    }
    "And here are the errors that I get:

    error C2296: '^' : illegal, left operand has type 'float'
    error C2297: '^' : illegal, right operand has type 'float'

    Does anyone have an idea of what I'm doing wrong?"
    -----------------------------------
    This is a good question which can be answered very easily because proper information was given. The answer is of course that the XOR operator ( ^ ) doesn't work with floating point types according to the C++ standard:

    "Each of the operands shall have integer type."

    The fixed code would look like this:
    Code:
    #include <iostream>
    
    int main()
    {
      int test = 1234;
      std::cout<< (test ^ test) <<'\n';
      return 0;
    }
    And the correct output of running the code should be 0.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    the first step in learning this stuff is to calm down. you will get it quickly.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    249
    Originally posted by ygfperson
    the first step in learning this stuff is to calm down. you will get it quickly.
    You mean quickly and safty,,,,,
    Don't think , let it go........? Ask me man?
    C++
    The best

  5. #5
    Unregistered
    Guest
    Code:
    #include <iostream.h>
    
    void Encrypt;
    
    int main( )
    {
    void (* pFunc);
    bool wQuit = false;
    
    int PickYouFool;
    while (wQuit == false)
    {
    cout << “(0)Quit (1)Encrypt:”;
    cin >> PickYouFool;
    switch (PickYouFool)
    {
    case 1: pFunk = Encrypt; break;
    default : wQuit = true; break;
    }
    if (wQuit)
    break;
    }
    return 0;
    }
    
    void Encrypt
    {
    /* need help here and anywhere else where there are facts of life */
    }
    Thanks to all who help, remember I’m a fragile newbie who has no self-esteem.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > void Encrypt;

    Should be:

    void Encrypt( );

    Or better yet:

    void Encrypt( void );



    > case 1: pFunk = Encrypt; break;

    This is supposed to do what exactly?

    Let's assume you use the same spelling
    across the board. This should be 'pFunc'.
    Honestly though, why are you even
    bothering using a function poitner? You
    have no reason to that I can see.


    void Encrypt
    {
    /* need help here and anywhere else where there are facts of life */
    }


    Um no. I don't know what method of Encryption you're planning on using, though from the title, I assume it's just some XORing, but I'm not going to write the entire function for you, especially since I'd just be guessing as to what it is you need done.

    You need to make your program requirements clearer. Then break it down into simple logical steps.

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

  7. #7
    Unregistered
    Guest

    Talking

    I was planning on adding a decrypter and some other things as I got better and became more fluent in the C++. It’s XORing but since I’m a noob I wouldn’t really know about other styles of encryption, so you can’t really blame me there. But don’t worry you still got me on pFunc, I thought that it didn’t matter how you spelt it. Thanks you for replying and for giving me void Encrypt(void).

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Unregistered
    I was planning on adding a decrypter <-- snip -->
    If you are using XOR encrpytion you problably won't need seperate code for encrpytion and decryption. Just run the data through the one function, and watch it magically encrpyt and decrpyt on it's own
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Unregistered
    Guest

    Smile

    Okay so now I don't need a decrypter, but how do I write a XORing program that will take the file you want to encrypt/decrpyt. That's the problem I don't know how to do that all I can do is....nothing that involves XOR with out copying someone else’s. There’s know fun in that, so an explanation would be sort of cool right..um..now. Thanks.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well first off, you have to prompt for a file name.
    Then you have to open the file.
    Then you have to read from it.
    Then you have to XOR.
    Then you have to write to another file.
    They you remove the original file.
    Then you rename the secondary file back to the first file name.

    Altrenately, you could opening for both reading and writing. Read, then back up, then write over that block, repeat.

    In order to XOR, you need something to XOR with. (A 'key'.) Some value to apply to each block of data you're XORing. Then you just simply XOR it. Example:

    int a = 10;
    int key = 54321;
    int result = 0;

    result = a ^ c;
    cout << "Result: " << result << endl;

    result = result ^ key; // ie: result ^= key;
    cout << "Result: " << result << endl;

    There. You have successfully encrypted the value of '10', and decrypted it again.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple of questions about XOR linked lists...
    By klawson88 in forum C Programming
    Replies: 5
    Last Post: 04-19-2009, 04:55 PM
  2. what the XOR!!
    By the bassinvader in forum C Programming
    Replies: 10
    Last Post: 08-11-2006, 02:08 PM
  3. Function program not working...
    By sirSolarius in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2003, 07:35 PM
  4. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM
  5. XOR lists?
    By bgrahambo in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2002, 10:02 PM