빅과 리틀 엔디안 변환에 조언 부탁드리니다
글쓴이: shean0 / 작성시간: 월, 2003/07/14 - 4:03오후
아래처럼 bmp파일을 읽어들여서 작업을 하는것을 window에서 만들었습니다.
그리고 이것을 unix로 바꾸는 과정에서...문제가 생겼습니다..엔디안 문제로 보이는데요..
즉 value=10; 이라고 주었을경우 이것이 big으로 저장되었을것이구요.
그리고 저는 이것을 wirte_file을 하여서 bmp파일을 만드는데.. 이때 각각의
멤버value 자체를 little로 바꾼다음에 file_wirte를 하여야하거든요.
음..
지금은 무식하게
예를 들면 아래처럼 함수를 만들었는데..음.. 좀 더 잘 만들수 없을까요?/
#include<stdio.h>
void change_big_little_buf(int *i,short int *s,char *buf,int choice)
{
char tmp[5];
int ii; short int ss;
if( choice ==4)
{
memcpy(tmp,i,4);
buf[3]=tmp[0];
buf[2]=tmp[1];
buf[1]=tmp[2];
buf[0]=tmp[3];
memcpy(i,buf,4);
memcpy(&ii,buf,4);/*<==검증 */
}
else if(choice ==2)
{
memcpy(tmp,s,2);
buf[1]=tmp[0];
buf[0]=tmp[1];
memcpy(s,buf,2);
memcpy(&ss,buf,2); /* 검증 */
}
}
int main()
{
int i; short s;
char tmp[5];
i=1;
//Sec=conv(Sor);
change_big_little_buf(&i,NULL,tmp,sizeof(i));
printf("[%d]",i);
return 1;
}
목적은 아래의 구조체의 각 멤버마다 현재 빅으로 입력한 값을 리틀로 변환해서
파일에 write하려는 목적입니다.
typedef struct _BITMAPINFOHEADER
{
unsigned long biSize;
unsigned long biWidth;
unsigned long biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned long biCompression;
unsigned long biSizeImage;
unsigned long biXPelsPerMeter;
unsigned long biYPelsPerMeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
} BMPIH ; bmih->biSize = sizeof(BMPIH); /* Size of InfoHeader = 40 */
bmih->biWidth = w; /* Bitmap Width */
bmih->biHeight = h; /* Bitmap Height */
bmih->biPlanes = 1; /* Number of Planes :always(1)*/
bmih->biBitCount = 8; /* Bits per Pixel */
bmih->biCompression = 0; /* Type of Compression */
bmih->biSizeImage = w*h; /* Size of Image */
bmih->biXPelsPerMeter = 0; /* horizontal resolution: Pixels/meter */
bmih->biYPelsPerMeter = 0; /* vertical resolution: Pixels/meter */
bmih->biClrUsed = 0; /* Number of actually used colors */
bmih->biClrImportant = 0; /* Number of important colors 0 = all */Forums:


그런식으로 하셔도 가능합니다. ^^혹시나, 꼭 빅을 리틀로 바꿀
그런식으로 하셔도 가능합니다. ^^
혹시나, 꼭 빅을 리틀로 바꿀 필요없이 환경에 맞게 사용하실려면
네트웍 함수 중의
ntohl, htonl 이런걸 살펴보시길 바랍니다. 각각,
네트워크오더(빅) 를 호스트로( 빅이나 리틀 )
호스트오더를(빅, 리틀) 를 네트워크( 빅 ) 으로 바꾸는 함수입니다.
그럼 고운 하루
=========================
CharSyam ^^ --- 고운 하루
=========================
[이 현상은 뭐죠?? 이해가 안되요??]
이렇게 바꾸었습니다..
#ifdef NT
bmfh->bfType = 0x4D42; /* BM Signature */
#endif
change_big_little_buf(0,19788,(char *)&bmfh->bfType,2);
즉 다시 유닉스에서 하면..
왜
4D42 ==>4C4D로 나오는지 이유를 모르겠어요.저는 이런식으로 지금까지 변환해서 사용했었는데요...
음... 갑자기 이런 현상이 나오니 당혹스럽네요...무슨 오류가 있는것이죠??
#include<stdio.h> void change_big_little_buf(int i,short int s,char *buf,int choice); int main() { unsigned short bfType; bfType=19788; /* 4D 42 */ change_big_little_buf(0,19788,(char *)&bfType,2); printf("%d",bfType); /* 19788 ==> 4C4D */ return 1; } void change_big_little_buf(int i,short int s,char *buf,int choice) { char tmp[5]; int ii; short int ss; if( choice ==4) { memcpy(tmp,&i,4); buf[3]=tmp[0]; buf[2]=tmp[1]; buf[1]=tmp[2]; buf[0]=tmp[3]; /* memcpy(&ii,buf,4);<==검증 */ } else if(choice ==2) { memcpy(tmp,&s,2); buf[1]=tmp[0]; buf[0]=tmp[1]; /* memcpy(&ss,buf,2); 검증 */ } }선언시에 packed도 해주었습니다. typedef struct __attribute__((packed)) _BITMAPFILEHEADER { unsigned short bfType; unsigned long bfSize; unsigned short bfReserved1; unsigned short bfReserved2; unsigned long bfOffBits; } BMPFH;그리고 말씀하신 부분은 ntohl으로 해도.. 현재 host가 network order 이므로 영향이 없지 않나요??지금 제가 하고자 하는것은 입력(big)값을 little로 변환 하는것인데요..
음.. 제가 정확하게 이해를 못한것인가??요...
조언부탁드립니다.
언제나 즐프를 꿈꾸며~
오타때문입니다...흑흑..
4D42 ==> 19778 인데
19788로 테스트 하였습니다..흑흑...
답변주신분께 감사드립니다....
언제나 즐프를 꿈꾸며~
19788 이면 4d4c 가 맞습니다. (리틀에서)빅이라면 4c4
19788 이면 4d4c 가 맞습니다. (리틀에서)
빅이라면 4c4d 일텐데요. 그럼 고운 하루되시길...
19788 을 %x로 한 번 출력해 보시길... 그럼 고운 하루
=========================
CharSyam ^^ --- 고운 하루
=========================
댓글 달기