如何打印出文件的二进制表示形式
How do you print out the binary representation of a file?
我正在尝试创建一个压缩程序,但我需要知道如何以二进制打开文件并打印出其内容的基础知识。
在名为"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 ++ = x
将x
转换为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
- 浮点数为 32 位和 64 位二进制表示形式
- 如何将uint8_t的二进制表示形式读取为数字
- 二进制表示,如何获得"相关位"?
- 了解浮点数的二进制表示形式
- 如何在 mfc c++ 中流式传输.zip文件夹的二进制表示形式
- 浮点逆二进制表示
- 以C++显示浮点类型的二进制表示形式
- 在二进制表示中将二进制 std::string 转换为 int
- 如何在计算机内存中打印长双重表示的二进制表示
- 将uint8_t转换为其二进制表示
- 与类型的二进制表示相关的技术词是什么?
- 以从 a 到 b 的数字的二进制表示形式计算 "1" 位数字的算法
- C++-如何获得十进制数的二进制表示
- 如何将一个数字的二进制表示中的所有1相加
- 涡轮 C 中的二进制表示
- 数据C++的二进制表示显示的数据不正确
- 二进制表示形式
- 打印有符号整数的二进制表示形式
- BigInt实现-将字符串转换为存储为无符号int的二进制表示
- 如何用非标准二进制表示法对数字进行算术运算