Here you don't take into account that on x86 and all the other common architectures integers are stored in little endian.

You have to use something that converts the big endian to little endian when it's needed (something like htonl for 64bit ints).