I have a trivial C program below which takes an int as input and dumps the memory layout to the terminal:
# cat foo.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int i, foo = atoi(argv[argc-1]);
unsigned char *c = (void*)&foo;
for (i = 0; i < 4; i++)
printf("%02x ", c[i]);
printf("\n");
return 0;
}
For example:
# ./foo 1
01 00 00 00
I would need it exactly in such format, that is, as hex dump separated by spaces and as native endian (in other words, how the decimal would be stored in mem).
Is there a way to achieve the same in e.g. bash? The closest I got so far is:
# echo 1 | xargs printf "%08x\n" | sed 's/../& /g'
00 00 00 01
But as can be seen this prints the hex value, but not how it would reside in memory on little endian.