Implementation of C lib ntohl()

 Thus, only the byte order of the network matters, and the network is big-endian: and host can be little or big endian

Intel x86 is little endian


#include <stdint.h>
#include <string.h>

uint32_t ntohl(uint32_t const net) {
    uint8_t data[4] = {};
    memcpy(&data, &net, sizeof(data));

    return ((uint32_t) data[3] << 0)
         | ((uint32_t) data[2] << 8)
         | ((uint32_t) data[1] << 16)
         | ((uint32_t) data[0] << 24);
}

Comments

Popular posts from this blog

Identify machine is little or big endian