0
votes

I have a 5*5 unsigned long matrix which I need to convert to a 1-D array of unsigned chars in C. The size of the matrix is 200 bytes and that of the char array 128 bytes.

I don't know how to go about it. Any help will be appreciated.

The signature of the function is given below:

void fromLongtochar(unsigned char* bytes, unsigned long* words){ }

I need to generate the bytes array.

Any help will be greatly appreciated. Thanks in advance

1
Not clear, but if the long type has values > 255, you cannot cast it into a char. Or, do you mean to take a 4-byte long and break it up into 4 char values? Please be specific. - OldProgrammer
Something here doesn't add up. If you mean truncating the values into chars the final array should be only 25 bytes. If you mean breaking up the longs into 8 chars each, the final array should still be 200 bytes. - Jud

1 Answers

0
votes

If all you're wanting to do is interact with the data as an array of chars, a simple cast of the pointer will do. Note that there's a pointer to a pointer so we can change the pointer itself inside the function.

void fromLongtoChar(unsigned char** bytes, unsigned long* words){ }
    &bytes = (char*) words;

unsigned char bytes[200];
unsigned long words[5][5];

// Populate the words matrix here

fromLongToChar(&bytes, words);