빅엔디안(big endian) 관련해서 질문입니다.
글쓴이: 익명 사용자 / 작성시간: 금, 2002/09/06 - 9:01오전
솔라리스에서
정수형변수 i에 1234567890 을 집어넣고,
메모리 4바이트를 16진수로 찍어봤는데.
예상결과는
499602D2 와 같이 나올것으로 생각했거든요.
그런데 2번째 , 4번째 바이트에서 앞에 ff들이 쭉 나오는 이유가 뭔지요?
실행 소스
#include
int main()
{
int i , j ;
char *p;
i = 1234567890;
p = (char*)&i;
for ( j = 0 ; j < sizeof (int) ; j++ )
printf("%d-0x%02x\n" , j , p[j] );
return 0;
}
실행결과
0-0x49
1-0xffffff96
2-0x02
3-0xffffffd2
Forums:
Re: 빅엔디안(big endian) 관련해서 질문입니다.
빅엔디안보다는 형변환하고 관련이 있는 내용 같습니다.
char *p;
를
unsigned char *p
로 바꿔보세요.
0x96 을 signed char로 보면 -106 이거든요. 이걸 int값인 -106으로 하려
면 0xffffff96 으로 되는 것이죠.
0x96 을 unsigned char로 보면 150이고 이걸 int값으로 바꾸면
0x00000096이 되겠구요.
unsigned char *p 로 안 바꾸고 printf()에서 조작하시려면
printf("%d-0x%02x\n" , j , (unsigned char)p[j] );
로 바꿔주시면 될듯합니다.
printf("%d-0x%02x\n" , j , (unsigned int)p[j] );
로 바꾸면 안되더군요. 이유는 생각해 보세여.
댓글 달기