Xor char*

This is a discussion on Xor char* within the C++ Programming forums, part of the General Programming Boards category; I want to xor string with prepared key before : Code: unsigned char key[8]; char *text; text = "testtesttesttest"; srand ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    69

    Xor char*

    I want to xor string with prepared key before :

    Code:
    unsigned char key[8];
    char *text;
    text = "testtesttesttest";
    
    srand ( time(NULL) );
    int lowest=0, highest=255;
    int range=(highest-lowest)+1; 
    for(int index=0; index<8; index++){ 
       key[index] = lowest+int(range*rand()/(RAND_MAX + 1.0));
    }
    
    for(int a=0; text[a] != NULL; a++)
    {
        text[a] ^= key[a%8];        
    }
    I was programming in delphi before and i had byte type which is unsigned char here as i read. Conversion was pretty easy but don't know how to do that in C++.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    671
    What's the problem? The code seems to be correct, despite of the fact that:
    Code:
    char *text;
    text = "testtesttesttest";
    Should be:
    Code:
    char text[] = "testtesttesttest";
    I never put signature, but I decided to make an exception.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Better yet,
    Code:
    char *text;
    text = "testtesttesttest";
    ...can be changed to...
    Code:
    std::string text = "testtesttesttest";
    Last edited by Elysia; 03-08-2011 at 03:37 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,269
    "Incorrect" is not the word for it as it is not incorrect to use a null-terminated character array, it's just possibly more error prone.
    I've been seeing a loss of distinction between fact and opinion lately from Elysia, which disappoints me a little.

    FYI, it's like the distinction between array [0..X] of char and string in Delphi, though in C++ you don't need to use StrPas to convert from array of char to string.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple XOR Program
    By dolfaniss in forum C Programming
    Replies: 8
    Last Post: 05-24-2010, 01:27 PM
  2. Couple of questions about XOR linked lists...
    By klawson88 in forum C Programming
    Replies: 5
    Last Post: 04-19-2009, 04:55 PM
  3. Simple Xor Encryption help
    By soadlink in forum Windows Programming
    Replies: 13
    Last Post: 10-18-2006, 11:51 AM
  4. Function program not working...
    By sirSolarius in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2003, 07:35 PM
  5. XOR lists?
    By bgrahambo in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2002, 09:02 PM

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