C++ read binary file and convert to hex

后端 未结 1 565
一向
一向 2020-12-18 04:33

I\'m having some problems reading a binary file and converting it\'s bytes to hex representation.

What I\'ve tried so far:

ifstream::pos_type size;
         


        
相关标签:
1条回答
  • 2020-12-18 04:35
    char *memblock;
    … 
    std::string tohexed = ToHex(memblock, true);
    …
    
    string ToHex(const string& s, bool upper_case)
    

    There's your problem, right there. The constructor std::string::string(const char*) interprets its input as a nul-terminated string. So, only the characters leading up to '\0' are even passed to ToHex. Try one of these instead:

    std::string tohexed = ToHex(std::string(memblock, memblock+size), true);
    std::string tohexed = ToHex(std::string(memblock, size), true);
    
    0 讨论(0)
提交回复
热议问题