data type conversion - converting uint8_t[4] to uint32_t and back again in C -
i wrote test convert uint8[4]
uint32_t
, how convert output of u32
original uint8[4]
? assume can done same way via bit shifting, i'm not sure how...
uint32_t u32(uint8_t b[4]){ uint32_t u; u = b[0]; u = (u << 8) + b[1]; u = (u << 8) + b[2]; u = (u << 8) + b[3]; return u; } void p(uint32_t value){ printf("\nuint32: %u\n",value); } int main(){ uint8_t b[4]; char a[4] = "test"; char tmp[32]; int i; for(i=0;i<4;i++){ b[i] = a[i]; sprintf(tmp,"%s%u",tmp,b[i]); } printf("uint8: "); for(i=0;i<4;i++) printf("%u",b[i]); uint32_t t2 = u32(b); p(t2); return 0; }
i tried little bit different , using known functions , not self implemented. main point teach bytes lay in memory interpretation give them depends on context. note there endianness issues 1 should take care. best lesson use bit-wise operations.
#include <stdint.h> #include <stdio.h> #include <arpa/inet.h> int main() { uint8_t a[4]={0x1,0x2,0x3,0x4}; uint32_t b = *((uint32_t*) a); /* turning array unit32 */ printf("0x%x\n",htonl(b)); /* turn uint32 array */ uint32_t c = htonl(b); uint8_t d[4] = {0}; printf("0x%x\n",c); (int i=0; i<4 ;++i) d[i] = ((uint8_t*)&c)[3-i]; (int i=0; i<4 ;++i) printf("0x%x\n",d[i]); return 0; }
the output is:
0x1020304 0x1020304 0x1 0x2 0x3 0x4
disassembling [you can load program using lldb or gdb]:
root# otool -tv a.out a.out: (__text,__text) section _main: 0000000100000e50 pushq %rbp 0000000100000e51 movq %rsp, %rbp 0000000100000e54 subq $0x30, %rsp 0000000100000e58 movl $0x0, 0xfffffffffffffffc(%rbp) 0000000100000e5f movl 0x12b(%rip), %eax 0000000100000e65 movl %eax, 0xfffffffffffffff8(%rbp) 0000000100000e68 movl 0xfffffffffffffff8(%rbp), %eax 0000000100000e6b movl %eax, 0xfffffffffffffff4(%rbp) 0000000100000e6e movl 0xfffffffffffffff4(%rbp), %edi 0000000100000e71 callq 0x100000f50 0000000100000e76 leaq 0x117(%rip), %rdi 0000000100000e7d movl %eax, %esi 0000000100000e7f movb $0x0, %al 0000000100000e81 callq 0x100000f66 0000000100000e86 movl 0xfffffffffffffff4(%rbp), %edi 0000000100000e89 movl %eax, 0xffffffffffffffe0(%rbp) 0000000100000e8c callq 0x100000f50 0000000100000e91 movl $0x0, %esi 0000000100000e96 movabsq $0x4, %rdx 0000000100000ea0 leaq 0xffffffffffffffec(%rbp), %rcx 0000000100000ea4 movl %eax, 0xfffffffffffffff0(%rbp) 0000000100000ea7 movq %rcx, %rdi 0000000100000eaa callq 0x100000f60 0000000100000eaf leaq 0xde(%rip), %rdi 0000000100000eb6 movl 0xfffffffffffffff0(%rbp), %esi 0000000100000eb9 movb $0x0, %al 0000000100000ebb callq 0x100000f66 0000000100000ec0 movl $0x0, 0xffffffffffffffe8(%rbp) 0000000100000ec7 movl %eax, 0xffffffffffffffdc(%rbp) 0000000100000eca cmpl $0x4, 0xffffffffffffffe8(%rbp) 0000000100000ed1 jge 0x100000efe 0000000100000ed7 movl $0x3, %eax 0000000100000edc subl 0xffffffffffffffe8(%rbp), %eax 0000000100000edf movslq %eax, %rcx 0000000100000ee2 movb 0xfffffffffffffff0(%rbp,%rcx), %dl 0000000100000ee6 movslq 0xffffffffffffffe8(%rbp), %rcx 0000000100000eea movb %dl, 0xffffffffffffffec(%rbp,%rcx) 0000000100000eee movl 0xffffffffffffffe8(%rbp), %eax 0000000100000ef1 addl $0x1, %eax 0000000100000ef6 movl %eax, 0xffffffffffffffe8(%rbp) 0000000100000ef9 jmpq 0x100000eca 0000000100000efe movl $0x0, 0xffffffffffffffe4(%rbp) 0000000100000f05 cmpl $0x4, 0xffffffffffffffe4(%rbp) 0000000100000f0c jge 0x100000f3c 0000000100000f12 leaq 0x7b(%rip), %rdi 0000000100000f19 movslq 0xffffffffffffffe4(%rbp), %rax 0000000100000f1d movzbl 0xffffffffffffffec(%rbp,%rax), %esi 0000000100000f22 movb $0x0, %al 0000000100000f24 callq 0x100000f66 0000000100000f29 movl %eax, 0xffffffffffffffd8(%rbp) 0000000100000f2c movl 0xffffffffffffffe4(%rbp), %eax 0000000100000f2f addl $0x1, %eax 0000000100000f34 movl %eax, 0xffffffffffffffe4(%rbp) 0000000100000f37 jmpq 0x100000f05 0000000100000f3c movl $0x0, %eax 0000000100000f41 addq $0x30, %rsp 0000000100000f45 popq %rbp 0000000100000f46 ret 0000000100000f47 nopw (%rax,%rax) __osswapint32: 0000000100000f50 pushq %rbp 0000000100000f51 movq %rsp, %rbp 0000000100000f54 movl %edi, 0xfffffffffffffffc(%rbp) 0000000100000f57 movl 0xfffffffffffffffc(%rbp), %edi 0000000100000f5a bswapl %edi 0000000100000f5c movl %edi, %eax 0000000100000f5e popq %rbp 0000000100000f5f ret
Comments
Post a Comment