Thread: Simple problem

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

    Simple problem

    We all know that if we have a pointer to a char like the char* name and name is initialized with the literal "guro" then by writing cout << name; the string guro will printed as is. I want to make a function like that:
    void bcpy( char name[MAX], char* pointer );

    it takes two arguments one is the array of chars representing the name, and the other is a pointer which after the computation of the function will point to the same string as the one of array, but i will be able to use a command like: cout << pointer; to print the whole string rather than do a for loop with the length of the array to print each char seperately.
    Is it possible? any help|

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Uhm, yeah.
    You can just do char* pointer = name, where you have already declared char name[MAX].

    EDIT: You don't actually have to do this. cout << name will also work. name is char* in this case
    Last edited by C_ntua; 10-02-2008 at 05:47 AM.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    Firstly C_ntua are u greek? (ntua is national technology university of athens)

    I have code a xor algorithm.Actually there are many topics in th forums but i have another problem. I use xor 'encryption' for a string stored in a char[MAX] and the result returns in a a pointer of chars. but if i write cout << *pointer only the first character appears!

    Code:
    #include<iostream>
    #include<stdio.h>
    
    #define MAX 30
    
    using namespace std;
    
    char* XorStr( char name[MAX] /* int name[MAX] */, unsigned int nlen, char key[MAX], unsigned int klen )
    {
          // name will have the form "\x45\xAB..."
          char* p = new char[MAX]; 
          char* n = p;
          
          for( unsigned int i = 0, k = 0; i < nlen; i++ )
          {
               p[i] = name[i] ^ key[k];
               k = (++k < klen ? k : 0 );      
          }
          p[nlen] = '\0';
          
          return n;
    }
    
    int main( int argc, char* argv[] )
    {
        //int input[MAX] = {0x14, 0x6, 0x9, 0x1b, 0x6, 0x6f, 0x1c}, if 1st arg is int name[MAX]
        char input[MAX] = "\x2b\x6\xe\x16\x2b\x6\x11\x72\x8\x1d\xb";
        char key[MAX] = "giorgos";
        char* o = NULL;
        char* pklen = key;
        char* pilen = input;
        
        o = XorStr( input, strlen(pilen), key, strlen(pklen) );
        cout << "Decrypted string is: ";
        for( unsigned int m = 0; m < strlen(pilen); m++ )
        {
             cout << char(o[m]);
        }
        cout << endl;
        delete(o);
        system("pause");
        return 0;
    }
    as u can see i'd like to use this function as follows:
    cout << XortStr( "\x34\xea\...", len1, "guro", len2 );

    but only first character will be returned. Any ideas how to fix it without using other #include, or otherwise any help?

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Ok, your code, that works fine should be like this:
    Code:
    int main( int argc, char* argv[] )
    {
        //int input[MAX] = {0x14, 0x6, 0x9, 0x1b, 0x6, 0x6f, 0x1c}, if 1st arg is int name[MAX]
        char input[MAX] = "\x2b\x6\xe\x16\x2b\x6\x11\x72\x8\x1d\xb";
        char key[MAX] = "giorgos";    
        cout << "Decrypted string is: " << XorStr( input, strlen(input), key, strlen(key) ) << endl;;
        system("pause");
        return 0;
    }
    Yeah, I am greek and I am about to take my degree as an Electrical and Computers Engineer from NTUA. And from the string "giorgos" I assume that you are also greek?

    EDIT: Post for any explanation.
    EDIT2: Sorry, I updated the code to work fine
    Last edited by C_ntua; 10-02-2008 at 07:39 AM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    yeap i'm greek too, and i study mathematics in patra...anyway thanks man, f***** simplicity was the key, once more. But can u explain me where the initial code failed?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The problem is that the encrypted string can contain embedded 0 characters, which in C strings mean the end of the string. Anything up to the first 0 is printed.

    So yes, you might need a loop.

    Or an algorithm that does the loop for you:
    Code:
    #include <algorithm>
    #include <iterator>
    
    ...
    copy(o, o + strlen(input), ostream_iterator<char>(cout, ""));
    But then in C++ land there is also a string object which, among other benefits, should get output entirely regardless of the embedded 0's (they are non-printing, though, so you might see a space instead).

    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    string XorStr( string input, const string& key)
    {
    
          for( unsigned int i = 0, k = 0; i < input.size(); i++ )
          {
               input[i] = input[i] ^ key[k];
               ++k;
               if (k >= key.size()) k = 0;
          }
          return input;
    }
    
    int main()
    {
        //int input[MAX] = {0x14, 0x6, 0x9, 0x1b, 0x6, 0x6f, 0x1c}, if 1st arg is int name[MAX]
        string input("giorgus");
        string key("giorgos");
        cout << "Decrypted string is: ";
        cout << XorStr(input, key);
        cout << endl;
        system("pause");
        return 0;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    I can see now that Anon's code is more suitable as i have to avoid via apropriate selection of the key the middle 0's in my encrypted string.
    The thing is i want to feed my function with hex codes as the input string, so is it possible to do with string object?

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, the string would be constructed from the same string literal, so the contents should be exactly the same as in the char array.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM