Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将文件解析为十六进制_C++ - Fatal编程技术网

C++ 将文件解析为十六进制

C++ 将文件解析为十六进制,c++,C++,假设我有一个文件.img。我想解析文件并用十六进制显示它。这是我在互联网上的代码参考。但应用程序显示为空。请帮我解决这个问题 int _tmain(int argc, _TCHAR* argv[]) { const char *filename = "test.img"; ifstream::pos_type size; char * memblock; ifstream file(filename, ios::in|ios::binary|

假设我有一个文件.img。我想解析文件并用十六进制显示它。这是我在互联网上的代码参考。但应用程序显示为空。请帮我解决这个问题

     int _tmain(int argc, _TCHAR* argv[])
      {
    const char *filename = "test.img";
    ifstream::pos_type size;
    char * memblock;
    ifstream file(filename, ios::in|ios::binary|ios::ate);
      if (file.is_open())
      {
        size = file.tellg();
        memblock = new char [size];
        file.seekg (0, ios::beg);
        file.read (memblock, size);
        file.close();

        std::string tohexed = ToHex(std::string(memblock, size), true);
        cout << tohexed << endl;
      }
      }


        string ToHex(const string& s, bool upper_case)  { 


    ostringstream ret;

    for (string::size_type i = 0; i < s.length(); ++i)
    {
        int z = (int)s[i];
        ret << std::hex << std::setfill('0') << std::setw(2) << (upper_case ? std::uppercase : std::nouppercase) << z;
    }

    return ret.str();
}
int-tmain(int-argc,_-TCHAR*argv[]
{
const char*filename=“test.img”;
ifstream::pos_类型大小;
char*memblock;
ifstream文件(文件名,ios::in | ios::binary | ios::ate);
if(file.is_open())
{
size=file.tellg();
memblock=新字符[大小];
file.seekg(0,ios::beg);
file.read(memblock,size);
file.close();
std::string tohexed=ToHex(std::string(memblock,size),true);

cout那太多代码了


它可以帮助定义一个带有
操作符的类,这是太多的代码了


运算符
定义一个类可能会有帮助,但应用程序显示为空。
这不是真的。显示的代码不会编译。文件实际打开了吗?因为如果它没有打开,什么都不会显示,听起来好像就是这样。在
的开头放一个断点,然后一步一步地通过y我们的代码。
但应用程序显示为空。
这不是真的。显示的代码将不会编译。文件实际打开了吗?因为如果它没有打开,什么都不会显示,而且听起来好像就是这样。在
main
的开头放置一个断点,然后单步执行代码。@Johnsyweb,这使得cast implicit。通过在不使用iostreams的情况下进行十六进制转换,我可以消除强制转换,并简化程序。
static char hextable[]=“0123456789abcdef”嗯,不,
istreambuf_迭代器
不起作用,但是从
char
unsigned char
的隐式转换可能没有什么值得大惊小怪的。的确,查找表方法也很简洁。@Johnsyweb,这使得转换隐式化了。我可以通过执行没有iostreams的x转换。
static char hextable[]=“0123456789abcdef”;s>4]Hmm,不,
istreambuf\u迭代器
不起作用,但是从
char
unsigned char
的隐式转换可能没有什么值得大惊小怪的。的确如此。查找表方法也很简洁。
#include <iostream>
#include <iterator>
#include <algorithm>
#include <iomanip>

struct hexchar {
    char c;
    hexchar( char in ) : c( in ) {}
};
std::ostream &operator<<( std::ostream &s, hexchar const &c ) {
    return s << std::setw( 2 ) << std::hex << std::setfill('0') << (int) c.c;
}

int main() {
    std::copy( std::istreambuf_iterator<char>( std::cin ), std::istreambuf_iterator<char>(),
        std::ostream_iterator< hexchar >( std::cout ) );
}