I am reading data from a file and I need to use this data as input to a function api call. The data is read in as a char* pointer, but the function api call takes int* only. So I need to convert the char* pointer to a int* pointer and still make sure the underlying data stays the same so that I can pass this data to the function.
Since the size of an int is 4 bytes while a char is 1 byte, will this work properly if I do something like going through the char* array and fitting 4 chars into a single int location, such as:
int *temp = new int[(size of the char*)/4];
byteIndex=0; // this is the location of where to put the char in the int
intArrayIndex // this is the index to the int pointer
for(i from char[0] to char[max) {
// put char(I) into int[intArrayIndex]'s byteIndex
int temp = char(I);
int shift = byteIndex*4;
int[intArrayIndex] = int[intArrayIndex] | (temp<<shift);
// reset byteIndex back to 0 when it reaches the last index
if(byteIndex==3)
byteIndex = 0;
//increment the intArrayIndex every 4th char
if(i%4==0)
intArrayIndex++;
}
Let me know if this doesnt make sense, thanks.
Since the size of an int is 4 bytes while a char is 1 byte, will this work properly if I do something like going through the char* array and fitting 4 chars into a single int location, such as:
int *temp = new int[(size of the char*)/4];
byteIndex=0; // this is the location of where to put the char in the int
intArrayIndex // this is the index to the int pointer
for(i from char[0] to char[max) {
// put char(I) into int[intArrayIndex]'s byteIndex
int temp = char(I);
int shift = byteIndex*4;
int[intArrayIndex] = int[intArrayIndex] | (temp<<shift);
// reset byteIndex back to 0 when it reaches the last index
if(byteIndex==3)
byteIndex = 0;
//increment the intArrayIndex every 4th char
if(i%4==0)
intArrayIndex++;
}
Let me know if this doesnt make sense, thanks.