본문 바로가기
프로그래밍/그외

엔디안 (바이트 오더링)과 구조체

by 써드아이 2017. 6. 21.


일단 엔디안과 바이트 오더링에 관해서...

http://lifeiseggs.tistory.com/382


구조체는 어떨까...

#include 

typedef struct {
    char red;
    char green;
    char blue;
    char alpha;
} color_t;


void func5( color_t color, unsigned char *number )
{
    unsigned short num_short = 0x1234;
    unsigned char num_char[2] = { 0x12, 0x34 };


    printf( "num_short = 0x%x\n", num_short );
    printf( "num_char[0], [1] 0x%x, 0x%x\n", num_char[0], num_char[1] );
    printf( "\n\n" );



#if 1
    unsigned char *ptr_short = (unsigned char*) &num_short;
    printf( "*(ptr_short+0) : 0x%x, *(ptr_short+1) : 0x%x\n", *(ptr_short+0), *(ptr_short+1) );
#endif


#if 1
    unsigned short *ptr_short = (unsigned short*) num_char;
    printf( "ptr_short : 0x%x\n", *ptr_short );
#endif


#if 1
    unsigned short char_short = 0;
    char_short = num_char[0] << 8 | num_char[1];
    printf( "char_short = 0x%x\n", char_short );
#endif

}


void func4( color_t color, unsigned char *number )
{
    int int_data = 0;
    unsigned char *ptr = NULL;

    //int_data = number[0] | number[1] << 8 | number[2] << 16 | number[3] << 24;
    int_data = ((number[0] >> 3) << 11) | ((number[1] >> 2) <<5) | (number[2] >> 3);

    ptr = (unsigned char*) &int_data;

    printf( "int_data(0x%8X)\n", int_data );
    printf( "&int_data + 0(0x%08X)\n", *(ptr+0) );
    printf( "&int_data + 1(0x%08X)\n", *(ptr+1) );
    printf( "&int_data + 2(0x%08X)\n", *(ptr+2) );
    printf( "&int_data + 3(0x%08X)\n", *(ptr+3) );
}


void func3( color_t color, unsigned char *number )
{
    color_t *ptr_color = NULL;

    ptr_color = (color_t*) number;
    printf( "color : 0x%08X\n", *ptr_color );
    printf( "color.r(0x%X), g(0x%X), b(0x%X), a(0x%X)\n", ptr_color->red, ptr_color->green, ptr_color->blue, ptr_color->alpha );
}


void func2( color_t color, unsigned char *number )
{
    unsigned short data = 0;
    unsigned short *ptr_short = NULL;

    data = *((unsigned short*) number);
    printf( "(unsigned short) *ptr : 0x%04X\n", data );

    data = (number[1] << 8) | number[0];
    printf( "(unsigned short ) number[1] | number[0]  : 0x%04X\n", data );

    ptr_short = (unsigned short*) &color;
    printf( "short  : 0x%08X\n", *ptr_short );
}


void func1( color_t color, unsigned char *number )
{
    unsigned long *ptr_long = NULL;
    unsigned char *ptr_char = NULL;
    unsigned short * ptr_short = NULL;

    ptr_long = (unsigned long *) &color;
    printf( "long  : 0x%08X\n", *ptr_long );


    ptr_char = (unsigned char *) &color;
    printf( "ptr_char + 0 : 0x%08X\n", *(ptr_char+0) );
    printf( "         + 1 : 0x%08X\n", *(ptr_char+1) );
    printf( "         + 2 : 0x%08X\n", *(ptr_char+2) );
    printf( "         + 3 : 0x%08X\n", *(ptr_char+3) );


    ptr_short = (unsigned short *) &color;
    printf( "ptr_short - &color : 0x%08X\n", *ptr_short );


    ptr_short = (unsigned short*) number;
    printf( "ptr_short - number : 0x%X, 0x%X\n", *ptr_short, *(ptr_short+1) );


    ptr_long = (unsigned long*) number;
    printf( "ptr_short - number : 0x%X, 0x%X\n", *ptr_short, *(ptr_short+1) );
}


int main( void )
{
    unsigned char number[4] = { 0x12, 0x34, 0x56, 0x78 };
    color_t color = { 0x12, 0x34, 0x56, 0x78 };


    printf( "\n\nfunc 1 ==================================================\n\n" );
    func1( color, number );

#if 1

    printf( "\n\nfunc 2 ==================================================\n\n" );
    func2( color, number );


    printf( "\n\nfunc 3 ==================================================\n\n" );
    func3( color, number );


    printf( "\n\nfunc 4 ==================================================\n\n" );
    func4( color, number );
#endif


    printf( "\n\nfunc 5 ==================================================\n\n" );
    func5( color, number );


    printf( "\n\n==================================================\n\n" );

    return 0;
}


결과

func 1 ==================================================

long  : 0x78563412
ptr_char + 0 : 0x00000012
         + 1 : 0x00000034
         + 2 : 0x00000056
         + 3 : 0x00000078
ptr_short - &color : 0x00003412
ptr_short - number : 0x3412, 0x7856
ptr_short - number : 0x3412, 0x7856


func 2 ==================================================

(unsigned short) *ptr : 0x3412
(unsigned short ) number[1] | number[0]  : 0x3412
short  : 0x00003412


func 3 ==================================================

color : 0x78563412
color.r(0x12), g(0x34), b(0x56), a(0x78)


func 4 ==================================================

int_data(0x    11AA)
&int_data + 0(0x000000AA)
&int_data + 1(0x00000011)
&int_data + 2(0x00000000)
&int_data + 3(0x00000000)


func 5 ==================================================

num_short = 0x1234
num_char[0], [1] 0x12, 0x34


char_short = 0x1234


==================================================


'프로그래밍 > 그외' 카테고리의 다른 글

[PyQt] QMainWindow  (0) 2019.06.24
getopt_long 함수 사용예제  (0) 2018.04.03
[SQL] sql 구문 몇가지  (0) 2014.06.13
[SQL] select 한 내용을 insert 하기  (0) 2014.06.12
MFC / Win32 프로그래밍 팁  (0) 2014.04.16