如何打印出文件的二进制表示形式

How do you print out the binary representation of a file?

本文关键字:二进制 表示 文件 何打印 打印      更新时间:2023-10-16

我正在尝试创建一个压缩程序,但我需要知道如何以二进制打开文件并打印出其内容的基础知识。

在名为"Tester.txt"的文本文件中,我有如下内容:

乔丹

在.cpp文件中,我有如下内容:

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main
{ 
    fstream istr;
    istr.open("Tester.txt", ios::binary);      
}

根据我在cplusplus参考中的理解,这使用流对象来打开二进制中指定的文件?

但是我被困在如何准确地"打印"出文件的第一个字节,即二进制中的字母M ?

我知道二进制中的M(大写字母)是01001101。

那么如何对M进行二进制计数呢?

谢谢

您会混淆数字和数字的表示形式,这可能是由于"二进制"这个词有时可以用来描述两者。当您以"二进制模式"打开文件时,这意味着您将看到文件中字节的原始值。与表示以2为基数的数字的"二进制"意义上的没有任何关系。

假设一个文件有"x"后跟一个换行符和一个返回符。在"二进制模式"下,您将看到三个字节大小的值,一个包含"x"的ASCII码,一个包含"换行符"的ASCII码,一个包含"返回"的ASCII码。这些是从文件中读取的可以用二进制表示它们,但也可以用十进制或十六进制表示它们,您仍然可以从文件中读取完全相同的

以"binary"格式读取文件决定了你读取的,而不是你如何表示它们。无论 Two表示为"2"(十进制)、"10"(二进制)还是"Two"(英文),两辆车都是相同的两辆车。

流的二进制输入/输出使用其成员函数read()write()完成。

:

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main
{ 
    fstream istr;
    istr.open("Tester.txt", ios::binary);
    if (istr) {
      // Read one byte
      char byte;
      if (!istr.read(&byte, 1)) {
        // Error when reading
      }
      // Alternative way to read one byte (thanks to DyP)
      byte = istr.get();
      // Another alternative:
      if (!istr.get(byte)) {
        // Error when reading.
      }
      // Read a block of bytes:
      char buffer[1024];
      if (!istr.read(buffer, 1024)) {
        // Read error, or EOF reached before 1024 bytes were read.
      }
    }
}

这是一个使用c++标准库完成所有繁重工作的快速程序。

#include <iostream>
#include <iterator>
#include <bitset>
#include <algorithm>
int main() {
    std::istreambuf_iterator< char > in( std::cin ), in_end;
    std::ostream_iterator< std::bitset< 8 > > out( std::cout, " " );
    std::copy( in, in_end, out );
    std::cout << 'n';
}

看它运行。我使用std::cin进行演示,但是您应该使用std::ios::binary打开一个文件并传递它。

由于每个变量只使用一次,因此可以在一行中完成所有操作。即使你打开文件而不是使用std::cin .


编辑:

  • std::copy是封装循环for ( ; in != in_end; ++ in ) * out ++ = * in;的函数。
  • 类型std::istreambuf_iterator要么接受istream构造函数实参并提供适合这种循环的迭代器in,要么不接受构造函数实参并提供迭代器in_end,使in == in_end成为in.eof() == true。迭代器从流中获取未格式化字节(类型为char)。
  • 类型std::ostream_iterator< std::bitset< 8 > >提供了一个迭代器out,因此* out ++ = xx转换为std::bitset< 8 >并打印结果。在这种情况下,x是一个字节,bitset为这样的字节值提供了一个构造函数,并重载operator<<来打印1和0的二进制表示。

由于标准库不支持该输出格式,因此需要手动输出二进制值。

int mask = 0x80;
while(mask)
{
    std::cout << (byteValue & mask ? '1' : '0');
    mask >>= 1;
}
std::cout << std::endl;

这将扫描从顶部到低位,并打印出一个值代表每一个

try this:

#include <fstream>
#include <iostream>
#include <string>
#include <bitset>
#include <iomanip>

int main()
{ 
    // Set up your objects.
    char          c;
    std::fstream  istr("Tester.txt", ios::binary);  
    unsigned long loc = 0;
    // Read the file one character at a time.
    // Remembering not to skip white space in this situation.
    for(;istr >> std::noskipws >> c;++loc)
    {
        // When printing compensate for non printable characters.
        // If the character is outside the ASCII range then print it as an integer.
        std::stringstream  charStr;
        if ((c < 32) || (c > 126))
        {
            charStr << "Char: " << c;
        }
        else
        {
            charStr << "Non Printable: " << static_cast<int>(c);
        }
        // Print the value and location in a nicely formatted way.
        std::cout << std::setw(16) << location 
                  << " : " 
                  << std::bitset<8>(c).to_string()  // Prints the character as an 8 bit binary value.
                  << " : "
                  << charStr.str()
                  << "n";
    }
}

但是已经有一些标准工具可以做到这一点:

查看od