2024.03.26
#include <stdio.h>
void main(){
unsigned char
a[]={'a','b','c','d','e','f','g','h'};
//1バイトの配列 コードだと0x61〜0x68
列 |
値 |
00 |
'a' |
01 |
'b' |
02 |
'c' |
03 |
'd' |
04 |
'e' |
05 |
'f' |
06 |
'g' |
07 |
'h' |
$ cat try1.c #include <stdio.h> void main(){ unsigned char a[]={'a','b','c','d','e','f','g','h'}; unsigned short int *i; i=(unsigned short int *)a; printf("%x\n",*i++); printf("%x\n",*i++); printf("%x\n",*i++); printf("%x\n",*i); } $ gcc try1.c $./a.out 6261 6463 6665 6867 |
$ cat try2.c #include <stdio.h> void main(){ unsigned short int i[]={0x6162,0x6364,0x6566,0x6768}; unsigned char *a; a=(unsigned char *)i; printf("%x\n",*a++); printf("%x\n",*a++); printf("%x\n",*a++); printf("%x\n",*a++); printf("%x\n",*a++); printf("%x\n",*a++); printf("%x\n",*a++); printf("%x\n",*a++); } $ gcc try2.c $ ./a.out 62 61 64 63 66 65 68 67 karappi@aogera:~/wrk/progi/3nen/exif$ |
上図の右のようにchar型配列buff[]に0x4d〜0x08のデータが入っている。これを構造体tifhedの型枠で読み取る
プログラム(抜粋)
略 struct tifhead{ unsigned short int endian; //2byte endian unsigned short int magic; //2byte magic number unsigned int zerothifd; //4byte 0th IFD addr }; 略 unsigned char buff[1024]; 略 fread(buff,1,8,fp); //ファイルからbuffに8バイト読み込む struct tifhead *thd; thd=(struct tifhead *)buff; printf("endian %04x\n",thd->endian); printf("magic %04x\n",thd->magic); printf("zerothifd %08x\n",thd->zerothifd); 略 |
実行結果(printfの出力)
endian 4d4d magic 2a00 zerothifd 08000000 |