2017-08-11 98 views
-3

好的。所以我写了这段代码,它所做的只是打印16个十六进制值,然后打印16个字符,然后为下一行打印换行符。在for循环中,我注意到我必须以某种方式使它打印127以上的任何东西,为什么我不知道,这就是为什么我在这里问你。cout奇怪的行为与cout?

最重要的是,任何人都有使用与十六进制cout任何指针?因为我不得不导入一个额外的模块只是为了设置cout的宽度为2.

这段代码刚刚完成了练习,我想输出看起来像它在十六进制编辑器HxD中的样子。我想我可以只使用一个字符串,而不必在字符串声明中复制两次相同的值。

但是,是的,任何指针或批评,请不要退缩。

#include <iostream> 
#include <fstream> 
#include <string> 

#define SIZE 0x830 

using namespace std; 

int main(int argc, char * argv[]) 
{ 

    int i, j, k; 

    ifstream dump(argv[1], ios::binary); 

    char* buffer = new char[SIZE]; 

    if (dump.is_open()) 
    { 
     dump.read(buffer, SIZE); 
     dump.close(); 
    } 
    else 
    { 
     cout << "no success :(\n"; 
     exit(0); 
    } 

    std::string my_hex; 
    std::string my_char; 

    for (int i = 0; i < SIZE; i++) 
    { 
     my_hex += buffer[i]; 
     my_char += buffer[i]; 
    } 

    for (int j = 0; j <= 2095; j++) //j is the position we are in the loop, which means that j also equals current character. 
    { 

     if ((j % 16 == 0) && (j != 0)) //if j is divisible by 16 and does not equal 0 then enter this if block, otherwise just print my_hex[j] 
     { 

      for (k = 0; k <= 15; k++) 
      { //handle regular characters 

       if ((my_char[(j + k) - 16] >= 0) && (my_char[(j + k) - 16] <= 31) || (my_char[(j + k)] == 255)) 
       { // this checks if the value is lower than 31 or equal to 255, either which can have a '.' printed in its place. 
        cout << "."; 
       } 
       else if ((my_char[(j + k) - 16] >= 32) && (my_char[(j + k) - 16] <= 127)) 
       { //if value is greater than or equal to 32 or equal to or less than 127 it will print that character, remember to print the correct character. we must go back to the beginning of the string with - 16. 
        cout << my_char[(j + k) - 16]; 
       } 
       else 
       { 
        cout << my_char[(j + k) - 16]; //prints everything above 127 
       }; 

      } 
      cout << "\n"; 
     } 
     printf("%02x ", (unsigned char) my_hex[j]); //print hexadecimal values of characters 
    } 
    dump.close(); 
    return 0; 
} 

如果你认为你知道更好的方法,那么请一定赐教:) *我曾在CMD交换代码页1252(ANSI)字符输出看的权利相比,HXD 。

+1

这是'std :: cout << std :: hex << 19 << std :: endl;'你在找什么? –

+1

使用'std :: hex'有什么问题? – user0042

+0

你的意思是在forloop条件。而不是'for(i = 0; i> = 2095; i ++)'而不是'for(i = 0; i> = length.my_hex; i ++)''? – james28909

回答

3

我相信你的代码太复杂了。

char * p_row = &buffer[0]; 

const unsigned int BYTES_PER_ROW = 16; 
unsigned int byte_count = 0; 
while (byte_count < size) 
{ 
    // Print the offset for the row, in hex 
    const size_t offset = (size_t)(p_row - &buffer[0]); 
    std::cout << std::hex << std::fill('0') << std::setw(6) 
       << offset; 
    std::cout << " "; 

    // Print hex values for each byte in the row 
    for (unsigned int i = 0; i < BYTES_PER_ROW; ++i) 
    { 
     const unsigned int byte = (uint8_t)(p_row[i]); 
     std::cout << std::hex << std::fill('0') << std::setw(2) 
       << byte; 
     if (i == 8) 
     { 
     std::cout << " "; 
     } 
    } 
    // Print ASCII values for printable characters or '.' 
    std::cout << "  "; 
    for (unsigned int i = 0; i < BYTES_PER_ROW; ++i) 
    { 
     char c = p[i]; 
     if (!isprint(c)) 
     { 
     c = '.'; 
     } 
     std::cout << c; 
    } 
    std::cout << "\n"; 
    p += BYTES_PER_ROW; 
    byte_count += BYTES_PER_ROW; 
} 

在上面的代码中,首先打印偏移量。
接下来,该行以2位十六进制格式打印。
最后,行被打印为可打印字符或'。'。

缓冲区指针和字节计数增加每行的字节数。

上述代码没有考虑不是BYTES_PER_ROW长度的行。这是一个简单的解决方法,并作为OP的练习。提示:使用istream::gcount来获取从文件中实际读取的字节数。

+0

谢谢。我将在法律的最大范围内检查这些代码。即时通讯使用'*'和'&不及格。如果你有一些简单的术语来帮助我理解,我将非常感激! – james28909

+0

我想要做的是打印任何可打印的字符和'。'为那些没有。但排队输出的数据是棘手的(对我来说)l – james28909

+0

使用你最喜欢的参考和查找'isprint'。您的'if'语句可以通过调用'isprint'来取代。 –