파일에서 이진데이터 8바이트만 읽어오고싶은데..잘안되네요

his2000x의 이미지

복합 문서 형식을 파싱해보려구 하는데요
한글파일도 복합문서 형식이라서 한글파일로
파일시작의 8바이트만 읽어오려고 합니다.

제가 코딩한소스는

typedef struct Header
{
	int identifier[8];
}TESTHeader;
 
if ( (fp = fopen("/home/test/2010.hwp","rb")) == NULL )
	{
		printf("cannot open file\n");
		fclose(fp);
	}
fread(&testHeader,sizeof(TESTHeader),1,fp);

이렇게 인데

복합문서의 헤더는 파일 제일 처음 시작이 d0 cf 11 e0 a1 b1 1a e1 이 온다구 문서에 써있어요

그래서 int identifier[8];로 선언하구 읽으면
identifier[0]에 e011cfd0 가 저장됩니다.
리틀엔디언이라서 거꾸로 저장된거맞죠??

그래서 char identifier[8]로 선언을 바꾸고 읽어봤습니다.
그러면 8바이트가 배열 8개에 나눠져서 들어오긴하는데

printf("%x\n",testHeader.identifier[0]); 
printf("%x\n",testHeader.identifier[1]); 

로 출력해보면
identifier[0] 은 ffffffd0
identifier[1] 은 ffffffcf
라고 나오네요.

왜 앞에 ffffff가 붙는건지 , 그리고 배열에 d0,cf 등만 저장되게 하려면 어떻게 읽어야하는지
감이 안잡히네요
힌트 주시면 감사히 배우겠습니다...
한수부탁드립니다..

세벌의 이미지

 identifier[0] & 0xff

이런 식으로 bitwise and 를 써 보시면 될 것 같습니다.
pyongye2009의 이미지

char는 제일 앞 bit가 부호비트이기 때문에 -128 ~ 127까지 박에 표시못합니다.

char를 BYTE로 바꾸어서 BYTE identifier[8]로 선언해야 하고요.
printf("%x\n",testHeader.identifier[0]); 이것은 printf("%02x\n",testHeader.identifier[0] ); 로 바꾸면 될 것 같네요.

shint의 이미지

unsigned char 로 하니 ffff가 사라집니다.

#include <stdio.h>
 
typedef struct Header
{
	int id[8];
}TH;
 
typedef struct Header2
{
	unsigned char id[8];
}TH2;
 
void main()
{
	{
	TH th;
	FILE* fp=NULL;
	if ( (fp = fopen("2011.hwp","rb")) == NULL )
	{
		printf("cannot open file\n");
		fclose(fp);
	}
	fread(&th,sizeof(TH),1,fp);
	fclose(fp);
 
	for(int i=0; i<8; i++)
	{
		printf("%x\n",th.id[i]);
	}
	}
	printf("\n");
 
	{
	TH2 th2;
	FILE* fp=NULL;
	if ( (fp = fopen("2011.hwp","rb")) == NULL )
	{
		printf("cannot open file\n");
		fclose(fp);
	}
	fread(&th2,sizeof(TH2),1,fp);
	fclose(fp);
 
	for(int i=0; i<8; i++)
	{
		printf("%x\n",th2.id[i]);
	}
	}
}
 
//파일 헤더
//d0 cf 11 e0 a1 b1 1a e1
 
//윈도우에서 실행
//int id[8]
//	[0]	0xe011cfd0
//	[1]	0xe11ab1a1
//	[2]	0x00000000
//	[3]	0x00000000
//	[4]	0x00000000
//	[5]	0x00000000
//	[6]	0x0003003e
//	[7]	0x0009fffe
 
//unsigned int id[8]
//	[0x0]	0xe011cfd0
//	[0x1]	0xe11ab1a1
//	[0x2]	0x00000000
//	[0x3]	0x00000000
//	[0x4]	0x00000000
//	[0x5]	0x00000000
//	[0x6]	0x0003003e
//	[0x7]	0x0009fffe
 
//char id[8]
//	[0x0]	0xd0 '?
//	[0x1]	0xcf '?
//	[0x2]	0x11 '.'
//	[0x3]	0xe0 '?
//	[0x4]	0xa1 '?
//	[0x5]	0xb1 '?
//	[0x6]	0x1a '.'
//	[0x7]	0xe1 '?

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

ymir의 이미지

%x 는 unsigned int 를 unsigned hexadecimal 로 출력하는 지시자입니다.
char 0xd0 를 unsigned int 로 assign 하면 0xffffffd0 가 됩니다.
출력할 때 char 변수를 (unsigned char) 로 캐스팅 하거나, %2x 처럼 precision 을 지정해주면 됩니다.
아니면, 변수를 그냥 unsigned char 로 선언해도 됩니다.
char 가 아닌 binary data 를 처리하는 경우에는, unsigned 로 처리하는것이 좋습니다.

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

his2000x의 이미지

감사합니다~
많이 배워갑니다~!!

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.