開発環境 |
Microsoft Visual C++ 2010 Express (SP1) |
実行環境 |
Microsoft Windows XP Home Edition (SP3) |
プロジェクトの種類 |
Win32 コンソール アプリケーション |
プロジェクト名 |
hexdmp |
アプリケーションの種類 |
コンソール アプリケーション |
追加のオプション |
空のプロジェクト |
hexdmp.c
/*
0 1 2 3 4 5 6 7
01234567890123456789012345678901234567890123456789012345678901234567890123456789
HHHHHHHH:HH HH HH HH-HH HH HH HH-HH HH HH HH-HH HH HH HH:XXXXXXXXXXXXXXXX
*/
#include <ctype.h>
#include <stdio.h>
#include <string.h>
typedef unsigned char u_char;
int main(int argc, char* argv[])
{
FILE* pFile;
u_char aucBuf[16];
u_char uc;
char acLine[80];
char acTmp[8+1];
int iCount;
int iRead;
int i;
if (argc != 2) {
fprintf(stderr, "usage: hexdmp file\n");
return 1;
}
if (fopen_s(&pFile, argv[1], "rb") != 0) {
fprintf(stderr, "fopen_s error\n");
return 1;
}
for (iCount = 0; ; iCount += 16) {
iRead = fread(aucBuf, 1, 16, pFile);
if (iRead <= 0) {
break;
}
strcpy_s(acLine, sizeof acLine,
" : - - - : ");
sprintf_s(acTmp, sizeof acTmp, "%.8X", iCount);
memcpy(acLine, acTmp, 8);
for (i = 0; i < iRead; i++) {
uc = aucBuf[i];
sprintf_s(acTmp, sizeof acTmp, "%.2X", uc);
memcpy(acLine + 9 + i * 3, acTmp, 2);
if (isprint(uc) == 0) {
uc = '.';
}
acLine[57 + i] = uc;
}
puts(acLine);
if (iRead != 16) {
break;
}
}
fclose(pFile);
return 0;
}
出力
00000000:68 65 6C 6C-6F 2C 20 77-6F 72 6C 64-09 68 65 6C:hello, world.hel
00000010:6C 6F 2C 20-77 6F 72 6C-64 0D 0A - :lo, world..
最終更新:2012年09月01日 16:52