64bit ntohl hton64

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

uint64_t ntoh64(const uint64_t *input)
{
    uint64_t rval;
    uint8_t *data = (uint8_t *)&rval;

    data[0] = *input >> 56;
    data[1] = *input >> 48;
    data[2] = *input >> 40;
    data[3] = *input >> 32;
    data[4] = *input >> 24;
    data[5] = *input >> 16;
    data[6] = *input >> 8;
    data[7] = *input >> 0;

    return rval;
}

uint64_t hton64(const uint64_t *input)
{
    return (ntoh64(input));
}

int main(void)
{
    uint64_t ull;

    ull = 1;
    printf("%Iu64\n", ull);

    ull = ntoh64(&ull);
    printf("%Iu64\n", ull);

    ull = hton64(&ull);
    printf("Iu64\n", ull);

    return 0;
}

你可能感兴趣的:(64bit ntohl hton64)