Simple problem

• 10-02-2008
guro
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|
• 10-02-2008
C_ntua
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
• 10-02-2008
guro
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?
• 10-02-2008
C_ntua
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
• 10-02-2008
guro
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?
• 10-02-2008
anon
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; }```
• 10-02-2008
guro
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?
• 10-02-2008
anon
Yes, the string would be constructed from the same string literal, so the contents should be exactly the same as in the char array.