i0nucleus.egloos.com

ZeroNucleus - Programmer


Twitter_FM

All_Blog_AD

English

Yahoo Blog Rank

야후 블로그 벳지


[C] int형변수(4byte)를 1byte로 접근방법(char casting)

[C] int형변수(4byte)를 1byte로 접근방법(char casting)

시프트연산과 &연산으로 결과를 얻을 수 있지만, 코드 량이 많아질 것 같아서 포인터를 사용하였습니다.

int형 변수(4Byte)를 char형(1Byte)로 하나씩 불러오는 방법입니다. 16진수 출력.
int형 변수를 char형으로 캐스팅 했습니다.   [Source Code 2]에서는 코드를 줄였습니다.

[Source Code 1]
#include <stdio.h>
int main()
{
    int nValue = 0x102030ff;     
    int *npValue;   
    unsigned char *cpValue;

    npValue = &nValue;
    cpValue = (unsigned char *)npValue;
   
    printf("%x\n", *(cpValue+3));
    printf("%x\n", *(cpValue+2));
    printf("%x\n", *(cpValue+1));
    printf("%x\n", *(cpValue+0));

    return 0;
}

[Result 1]
10
20
30
ff
계속하려면 아무 키나 누르십시오 . . .



[Source Code 2]
#include <stdio.h>
int main()
{
    int nValue = 0x102030ff;     
    unsigned char *cpValue;

    cpValue = (unsigned char *)(&nValue);
   
    printf("%x\n", *(cpValue+3));
    printf("%x\n", *(cpValue+2));
    printf("%x\n", *(cpValue+1));
    printf("%x\n", *(cpValue+0));

    return 0;
}

[Result 2]
10
20
30
ff
계속하려면 아무 키나 누르십시오 . . .


트랙백

이 글과 관련된 글 쓰기 (트랙백 보내기)
TrackbackURL : http://i0nucleus.egloos.com/tb/2617800 [도움말]

덧글

  • SouL 2009/10/11 23:37 # 답글

    초보프로그래머입니다 ^^ 링크 납치해갑니다!
  • i0Nucleus 2009/10/12 22:54 #

    안녕하세요. ^^ 납치(?) 햐햐~
※ 로그인 사용자만 덧글을 남길 수 있습니다.