I want to convert an array of int to an array of char and then back again, for serialization purposes. It does not need to work cross-platform.
I came up with
//sample int array
int arr[] = {1,2,3,4,100};
char char_arr[sizeof(arr)];
//int arr to char arr
memcpy(char_arr, arr, sizeof(arr));
int newarr[sizeof(char_arr)/sizeof(int)];
//char arr back to int arr
memcpy(newarr, char_arr, sizeof(char_arr)/sizeof(int));
This does not seem to work however (newarr contains values different from arr). Any suggestions?
newarrthe size when you copy to it is stillsizeof(char_arr), i.e. do not divide bysizeof(int).sizeof(int)in the call tomemcpy.reinterpret_cast<const char *>(arr)will suffice for your application?