C++ 언어 소스:

// Filename: testHexView02.cpp
//
//  Chapter 1.  Section 1.
//
//       A basic pattern of C++ Programing
//
// Compile: cl /EHsc testHexView02.cpp
// Compile: g++ -std=c++11 -o testHexView02 testHexView02.cpp
// Compile: g++ -std=c++0x -o testHexView02 testHexView02.cpp


#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

void toHex(char c, char a[3]);
void toHex4(int n, char a[5]);
void toHex8(int n, char a[10]);

int main(int argc, char *argv[])
{
     if (argc < 2)
    {
        cout << "Usage: testHexView02 [filename]" << endl;
        exit( 1 );
    }

    const char *fname = argv[1];
    
    ifstream in(fname,ios::in | ios::binary | ios::ate);
 
    if (!in)
    {
        cout << "Fail to open the file: \"" << fname << ":\"." << endl;
        exit( 1 );
    }

    ifstream::pos_type fsize;

    char s[3];
    char t[10];
    char buf[16];
    fpos_t n = 0L;

    s[2] = '\0';
    t[4] = ' ';
    t[9] = '\0';
    if (in.is_open())
    {
        fsize = in.tellg();
        cout << "Its file size is " << fsize << "." << endl << endl;

        in.seekg (0, ios::beg);

        while (n < fsize && !in.eof())
        {
            toHex8(n, t);
            cout << t <<": ";
      
            in.read(buf, sizeof(buf));
            char x;
            long m = (((fsize - n) < 16L) ? (long) (fsize - n) : 16L);
            for (int i = 0; i < m; i++)
            {
                x = buf[i];
                toHex(x, s);
                cout << s;
                if (i == 7)
                    cout << "-";
                else
                    cout << " ";
            }
            if (m < 16)
            {
                for (int i = 0; i < 16 - m; i++)
                {
                    cout << "   ";
                }
            }

            cout << "|";
            for (int i = 0; i < m; i++)
            {
                if (buf[i] < ' ')
                    cout << ".";
                else
                    cout << buf[i];
            }

            if (m < 16)
            {
                for (int i = 0; i < 16 - m; i++)
                {
                    cout << " ";
                }
            }
            cout << "|";

            n += m;
            cout << endl;
        }

        in.close();
     
        cout << endl;
        cout << "Read " << n << " bytes" << endl;
    }
    else {
        cout << "Fail to read the file: \"" << fname << "\"." << endl;
    }

}

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 toHex4(int n, char a[5]) {
     int x1, x2, x3, x4;
    x1 = (n & 0xF000) >> 12;
    x2 = (n & 0xF00) >> 8;
    x3 = (n & 0xF0) >> 4;
    x4 = 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';
}

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';
}

 

 

명령창에서 Visual C++로 컴파일하기> cl /EHsc testHexView02.cpp


 

MinGW의 g++로 컴파일하기 위해 PATH를 설장하는 일괄파일(bat 파일)의 내용:

set path=d:\MinGW480\bin;d:\MinGW480\MSYS\1.0\bin;%path%

 

MinGW애서 컴파일하기> g++ -std=c++11 -o testHexView02 testHexView02.cpp

 

실행 예 1> testHexView02 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> testHexView02 example.bin
Its file size is 21.

0000 0000: 41 01 00 00 00 42 29 40-00 02 00 00 00 43 00 00 |A....B)@.....C..|
0000 0010: 00 03 00 00 00                                  |.....           |

Read 21 bytes

 

 

Posted by Scripter
,