C 언어 소스:
// Filename: testHexView_02.c
//
// Chapter 1. Section 1.
//
// A basic pattern of C++ Programing
//
// Compile: gcc -o testHexView_02 testHexView_02.c
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
void toHex(char c, char a[3]);
void toHex8(int n, char a[10]);
int main(int argc, char *argv[])
{
char *fname;
FILE *file2;
fpos_t fsize;
long m;
int i;
char x;
char s[3];
char t[10];
char buf[16];
fpos_t n = 0L;
struct stat stt;
if (argc < 2)
{
printf("Usage: testHexView_02 [filename]\n");
exit( 1 );
}
fname = argv[1];
if (stat(fname, &stt) == 0 )
{
if ( stt.st_mode & S_IFDIR )
{
printf("\"%s\" is a directory\n", fname);
exit( 1 );
}
}
if (access(fname, R_OK) != 0)
{
printf("The file: \"%s\" is not readble.\n", fname);
exit( 1 );
}
file2 = fopen(fname, "rb");
if (!file2)
{
printf("Fail to open the file: \"%s\".\n", fname);
exit( 1 );
}
s[2] = '\0';
t[4] = ' ';
t[9] = '\0';
if (file2)
{
fseek(file2, 0, SEEK_END); // seek to end of file
fsize = ftell(file2); // get current file pointer
printf("Its file size is %ld.\n\n", fsize);
fseek(file2, 0, SEEK_SET);
while(n < fsize && !feof(file2))
{
toHex8(n, t);
printf("%s: ", t);
fread(buf, sizeof(buf), 1, file2);
m = (((fsize - n) < 16L) ? (long) (fsize - n) : 16L);
for (i = 0; i < m; i++)
{
x = buf[i];
toHex(x, s);
printf("%s", s);
if (i == 7)
printf("-");
else
printf(" ");
}
if (m < 16)
{
for (i = 0; i < 16 - m; i++)
{
printf(" ");
}
}
printf("|");
for (i = 0; i < m; i++)
{
if (buf[i] < ' ')
printf(".");
else
printf("%c", buf[i]);
}
if (m < 16)
{
for (i = 0; i < 16 - m; i++)
{
printf(" ");
}
}
printf("|");
n += m;
printf("\n");
}
fclose(file2);
printf("\n");;
printf("Read %ld bytes.\n", n);
}
else {
printf("Fail to read the file: \"%s\".,", fname);
}
}
void toHex(char c, char a[3]) {
int x1, x2;
x1 = (c & 0xF0) >> 4;
x2 = c & 0x0F;
if (x1 < 10)
a[0] = x1 + '0';
else
a[0] = (x1 - 10) + 'A';
if (x2 < 10)
a[1] = x2 + '0';
else
a[1] = (x2 - 10) + 'A';
}
void toHex8(int n, char a[10]) {
int x1, x2, x3, x4, x5, x6, x7, x8;
x1 = (n & 0xF0000000) >> 28;
x2 = (n & 0xF000000) >> 24;
x3 = (n & 0xF00000) >> 20;
x4 = (n & 0xF0000) >> 16;
x5 = (n & 0xF000) >> 12;
x6 = (n & 0xF00) >> 8;
x7 = (n & 0xF0) >> 4;
x8 = n & 0x0F;
if (x1 < 10)
a[0] = x1 + '0';
else
a[0] = (x1 - 10) + 'A';
if (x2 < 10)
a[1] = x2 + '0';
else
a[1] = (x2 - 10) + 'A';
if (x3 < 10)
a[2] = x3 + '0';
else
a[2] = (x3 - 10) + 'A';
if (x4 < 10)
a[3] = x4 + '0';
else
a[3] = (x4 - 10) + 'A';
a[4] = ' ';
if (x5 < 10)
a[5] = x5 + '0';
else
a[5] = (x5 - 10) + 'A';
if (x6 < 10)
a[6] = x6 + '0';
else
a[6] = (x6 - 10) + 'A';
if (x7 < 10)
a[7] = x7 + '0';
else
a[7] = (x7 - 10) + 'A';
if (x8 < 10)
a[8] = x8 + '0';
else
a[8] = (x8 - 10) + 'A';
}
MinGW의 gcc로 컴파일하기> gcc -o testHexView_02 testHexView_02.cpp
실행 예 1> testHexView_02 temp_1.bin
Its file size is 12.
0000 0000: 48 65 6C 6C 6F 20 74 68-65 72 65 0A |Hello there. |
Read 12 bytes.
실행 예 2> testHexView_02 myFile.ser
Its file size is 130.
0000 0000: AC ED 00 05 73 72 00 06-50 65 72 73 6F 6E 07 31 |....sr..Person.1|
0000 0010: 46 DB A5 1D 44 AB 02 00-03 49 00 03 61 67 65 4C |F...D....I..ageL|
0000 0020: 00 09 66 69 72 73 74 4E-61 6D 65 74 00 12 4C 6A |..firstNamet..Lj|
0000 0030: 61 76 61 2F 6C 61 6E 67-2F 53 74 72 69 6E 67 3B |ava/lang/String;|
0000 0040: 4C 00 08 6C 61 73 74 4E-61 6D 65 71 00 7E 00 01 |L..lastNameq.~..|
0000 0050: 78 70 00 00 00 13 74 00-05 4A 61 6D 65 73 74 00 |xp....t..Jamest.|
0000 0060: 04 52 79 61 6E 73 71 00-7E 00 00 00 00 00 1E 74 |.Ryansq.~......t|
0000 0070: 00 07 4F 62 69 2D 77 61-6E 74 00 06 4B 65 6E 6F |..Obi-want..Keno|
0000 0080: 62 69 |bi |
Read 130 bytes.
'프로그래밍 > C' 카테고리의 다른 글
long long 타입의 정수를 printf 함수로 출력하기 (0) | 2013.12.03 |
---|---|
C 언어에서 동작하는 쓰레기 수집기(Garbage collector) (0) | 2013.10.19 |
Visual C 2010 으로 컴파일하여 실행해 본 OpenGL 예제: Redbook 의 Teapots (0) | 2013.05.17 |
Visual C 2010 으로 컴파일하여 실행해 본 OpenGL 예제: Redbook 의 Cube (0) | 2013.05.10 |
gcc 와 g++ 의 log 계산 오류 (0) | 2013.02.25 |