2004년 5월 10일 월요일

little endian <-> big endian converter (unsigned long long int일 경우)

unsigned long long int ull_endian_convert(unsigned long long int in_val)
{
    char c0;
    char c1;
    char c2;
    char c3;

    union conv_type {
        unsigned long long int ull;
        unsigned char c[8];
    } conv;
    conv.ull = in_val;
    c0 = conv.c[0];
    c1 = conv.c[1];
    c2 = conv.c[2];
    c3 = conv.c[3];
    conv.c[0] = conv.c[7];
    conv.c[1] = conv.c[6];
    conv.c[2] = conv.c[5];
    conv.c[3] = conv.c[4];
    conv.c[4] = c3;
    conv.c[5] = c2;
    conv.c[6] = c1;
    conv.c[7] = c0;

    return conv.ull;
}

------------------------------------------------
network byte order = big endian (더 직관적)
big endian을 반대로 뒤집으면 little endian

Linux, DOS = little endian
SUN = big endian

댓글 없음:

댓글 쓰기