I tried to do it a little bit different and using known functions and not self implemented. My main point is to teach you that bytes lay in the memory the interpretation you give them depends on context. Note there are some endianness issues one should take care. Moreover the best lesson is to 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 an array to unit32 */
printf("0x%x\n",htonl(b));
/* turn an uint32 to array */
uint32_t c = htonl(b);
uint8_t d[4] = {0};
printf("0x%x\n",c);
for (int i=0; i<4 ;++i)
d[i] = ((uint8_t*)&c)[3-i];
for (int i=0; i<4 ;++i)
printf("0x%x\n",d[i]);
return 0;
}
The output is:
0x1020304
0x1020304
0x1
0x2
0x3
0x4
Disassembling it [you can load the 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