c - how to read memory bytes one by one in hex(so without any format) with printf() -
i'm interested read 1 byte of memory in c.im using netbeans on ubuntu , below code read 1 byte(not whole value of a). nothing printed out on screen.
main() { int a[1]={3}; //printf(%d",a[0]); //line 2 printf("\x07",a[0]); //line 3 } in idea, memory in address label composed of:
- 0x0004 03
- 0x0008 00
- 0x000c 00
- 0x000f 00
printf() statement in line 2 indicates go address 0x???4 , :
- read 4 bytes (because of d character)
- represent these 4 bytes number meaning multiply them power of 2 (because of d character)
printf() statement in line 3 use \ (and not %) , bits 0 7 (1 byte). indicates that, go address 0x0004 and:
- list item
- read 1 byte (because of 07 characters) 2-do nothing except show each 4-bit hex value
so code should print out first byte in 0x0004 03. not. clue?
thanks in advance
please don't correct syntax. think hypotheses correct regarding formatters in printf?
use pointer address individual bytes of a , change printf use %x format specifier, e.g.:
int main(void) { int = 3; unsigned char *p = (unsigned char *)&a; int i; printf("a ="); (i = 0; < sizeof(a); ++i) { printf(" %02x", p[i]); } printf("\n"); return 0; } on little endian machine 32 bit ints should produce following output:
a = 03 00 00 00
Comments
Post a Comment