简体   繁体   English

逐字节读取任何文件(不是.bin文件)

[英]Reading any file (not .bin file) byte by byte

I want to read any file (.bin, .txt, .jpg, .zip, .mp3 etc.) byte by byte (or bit by bit) and display it on the console (in a format like 00100011). 我想按字节(或一点一点)读取任何文件(.bin,.txt,.jpg,.zip,.mp3等)并将其显示在控制台上(格式如00100011)。 There are some questions answered in the website but it is mostly about .bin files. 该网站上回答了一些问题,但主要是有关.bin文件的问题。 It should not matter which file format I work with. 我使用哪种文件格式都没有关系。 For example, when you open a .png file in a text editor, you see weird characters on the screen like "∑P®pT™5à*" and I presume these are every 8 bits of the file turned into ASCII letters and displayed on the editor (please correct me if I am wrong). 例如,当您在文本编辑器中打开.png文件时,您会在屏幕上看到“ ∑P®pT™5à*”之类的怪异字符,我想这些是文件的每8位变成ASCII字母并显示在编辑(如果我写错了,请纠正我)。

I am writing this program in c++ and so far I tried 我正在用C ++编写此程序,到目前为止我尝试了

fstream file("foo.txt", ios_base::binary);

to read the file in binary mode and get 8 bits of chunks, but this only works for the .txt files and it just displays the characters in the text file like it would normally do. 以二进制模式读取文件并获取8位块,但这仅适用于.txt文件,并且仅像通常那样在文本文件中显示字符。 However does not even work or open other file formats like .png . 但是, 甚至无法正常工作或打开.png等其他文件格式

Can I get some hints about how can I achieve this, and please correct me if I gave any wrong information. 我可以得到一些有关如何实现此目标的提示,如果我提供了任何错误的信息,请纠正我。

The issue is that only a portion of values in a byte are printable. 问题在于字节中只有一部分值是可打印的。 For example, the value 0x03 is not printable, but 0x42 is. 例如,值0x03是不可打印的,但0x42是可打印的。

I recommend that you cast the variable from uint8_t to unsigned int before printing. 我建议您在打印之前将变量从uint8_tunsigned int Something like cout << hex << (unsigned int)(value) << endl; cout << hex << (unsigned int)(value) << endl;

Also, don't use char , signed char or unsigned char when reading binary files. 另外,在读取二进制文件时,请勿使用charsigned charunsigned char Use uint8_t , uint16_t or uint32_t . 使用uint8_tuint16_tuint32_t

You are probably assigning the values to a "char" datatype. 您可能正在将值分配给“ char”数据类型。 You should always use unsigned types ("unsigned char" should suffice for your case) because there are no negative values for binary files and you will be able to read 0-255 instead of just 0-127(text characters). 您应该始终使用无符号类型(“ unsigned char”足以满足您的情况),因为二进制文件没有负值,并且您将能够读取0-255而不是0-127(文本字符)。 Then, if you want it displayed in binary, you can use this: 然后,如果要以二进制形式显示它,则可以使用以下命令:

unsigned char c = 251;
char binary[8];
itoa(c, binary, 2);
cout << binary << endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM