Alternatively, we can use C / C ++ casting to interpret the char buffer as an unsigned int array. This can help avoid bias and judgment.
#include <stdio.h> int main() { char buf[256*4] = "abcd"; unsigned int *p_int = ( unsigned int * )buf; unsigned short idx = 0; unsigned int val = 0; for( idx = 0; idx < 256; idx++ ) { val = *p_int++; printf( "idx = %d, val = %d \n", idx, val ); } }
This will print 256 values, the first of which will be idx = 0, val = 1684234849 (and all other numbers = 0).
As a side note, “ABCD” is converted to 1684234849 because it runs on X86 (Little Endian), in which “ABCD” is 0x64636261 (with 'a' is 0x61, and 'd' is 0x64 - in Little Endian, LSB is in lowest address). So 0x64636261 = 1684234849.
Note that when using C ++ in this case, reinterpret_cast should be used:
const char *p_buf = "abcd"; const unsigned int *p_int = reinterpret_cast< const unsigned int * >( p_buf );
source share