Sunday, July 12, 2009

Convert an int to a byte array in C?

How do I convert an int into a byte array in C?

Convert an int to a byte array in C?
hay write better maybe people will text you
Reply:A byte is 8 bits, where as an int is something bigger.





I would suggest using a pointer.


byte* ptr; //create a byte pointer


byte array[4]; //Your byte array


int x = 2; //Your integer


ptr = %26amp;x; //Ptr gets address of x, or the address where the integer is.


array[0] = *ptr; //Dereference ptr and strore first byte in array


ptr++; //increment pointer to point to the next byte in memory, or the next byte in the integer


array[1] = *ptr; //store the next byte,,, And so on.


No comments:

Post a Comment