簡體   English   中英

十六進制轉儲實用程序C ++顯示十六進制和ASCII

[英]Hex Dump Utility C++ Displaying Hex and Ascii

好的,所以我調用讀取文件來填充斜坡槽,但它僅適用於.txt或.bin文件,並且在我打開其他任何內容(例如錯誤代碼為0xC0000005的.doc文件)時會崩潰:執行位置0xFFFFFFFF的訪問沖突。

這也是顯示屏幕的十六進制和ascii轉儲的正確方法嗎?

//The slop_trough I dump all the hex and ascii slop into
unsigned char slop_trough [MAX] = {0} ;

void ReadFile (string filename, unsigned char slop_trough [])
{
ifstream open_bin;
open_bin.open(  filename, ios::in | ios::binary  ) ;
if ( open_bin.is_open() )
{
    open_bin.read ( reinterpret_cast <char *> (slop_trough),
        sizeof(slop_trough) * MAX ) ;

    open_bin.close();
}
else
    cout << "File not opened!" << endl;

}

void HexDump (unsigned char slop_trough [])
{
for ( int j = 0; j < MAX - 1; ++j)
{
   cout << hex << slop_trough[j] ;
}
}

void AsciiDump(unsigned char slop_trough [])
{
for ( int p = 0; p < MAX - 1; ++p)
{
   cout << slop_trough[p];
}
}

嘗試這樣的事情:

streamsize ReadFile (const string &filename, unsigned char *slop, int max_slop)
{
    ifstream open_bin;

    open_bin.open( filename, ios::in | ios::binary  ) ;
    if ( !open_bin.is_open() )
    {
        cout << "File not opened! " << filename << endl;
        return 0;
    }

    if( !open_bin.read ( reinterpret_cast<char*>(slop), max_slop ) )
    {
        cout << "File not read! " << filename << endl;
        return 0;
    }

    return open_bin.gcount();
}

void HexDump (unsigned char *slop, streamsize slop_len)
{
    for ( int j = 0; j < slop_len; ++j)
    {
        cout << noshowbase << hex << setw(2) << setfill('0') << slop[j] << ansi::reset();
        cout.put(' ');
    }
    cout << dec << setw(0) << setfill(' ');
}

void AsciiDump(unsigned char *slop, streamsize slop_len)
{
    for ( int p = 0; p < slop_len; ++p)
    {
       if ( (slop[p] > 0x20) && (slop[p] < 0x7F) && (slop[p] != 0x0A) && (slop[p] != 0x0D) )
           cout << slop[p];
       else
           cout << '.';
    }
    cout << ansi::reset();
}

//The slop_trough I read the file data into
unsigned char slop_trough [MAX] = {0} ;

streamsize slop_len = ReadFile ( "filename", slop_trough, MAX );
unsigned char *slop = slop_trough;

while ( slop_len >= 16 )
{
    HexDump ( slop, 16 );
    cout << "| "; 
    AsciiDump( slop, 16 );
    cout << endl;

    slop += 16;
    slop_len -= 16;
}

if ( slop_len > 0 )
{
    HexDump ( slop, slop_len );
    cout << left << setw(48 - (slop_len * 3)) << setfill(' ') << "| ";
    AsciiDump( slop, slop_len );
}

請看以下示例,以獲取更強大的十六進制查看器實現:

http://www.cplusplus.com/articles/NwTbqMoL/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM