Question

I'm working on C and I have a question.. I'm writing a program with Mcrypt and the decryption function require the chipertext at a char *pointer. The problem is that the chipertext is filled with unknown ASCII symbols.. So I printf the chipertext with %d and I get chipertext in numbers like this..

23 -83 -48 -36 -49 -26 -16 -42 101 111 127 -46 -10 -3 -33 110 -106 29 -112 123 -21 43 50 81 70 -101 -71 94 -63 -122 52 76

My question is.. I take this chipertext and store it in to a int array[32].. How can I copy the contents of this int array to my char *pointer?

Was it helpful?

Solution

How to copy an array of int to an array of char:

int iarray[32];
char carray[sizeof(iarray)];

memcpy(carray, iarray, sizeof(iarray));

If carray is a pointer instead of an array, the code remains largely the same:

int iarray[32];
char *carray = addressOfSomeMemory;

memcpy(carray, iarray, sizeof(iarray));

However, the code above assumes your data is stored as chars packed into ints. If instead your data is stored one char per int, you need more code:

for (int i = 0; i < 32; i++)
    *(carray+i) = (char)iarray[i];

OTHER TIPS

I think the easiest solution for you would be to store your cyphertext in char array[32].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top