小编The*_*ore的帖子

文本文件的二进制输入

编程原理与实践在第 11 章中说:“在内存中,我们可以将数字 123 表示为整数值(每个 int 占 4 个字节)或字符串值(每个字符占 1 个字节)”。

在读取二进制文本文件时,我试图了解内存中存储的内容。所以我正在写向量 v 的内容。

如果输入文件包含此文本:“test these words”

输出文件显示这些数字: 1953719668 1701344288 1998611827 1935962735 168626701 168626701 168626701 168626701 160616706

我试图将“测试”的每个字符转换为二进制,我有 01110100 01100101 01100101 01110100,如果我认为这是一个 4 字节的整数并将其转换为十进制,我得到 1952802164,这仍然与输出不同。

这是如何正确完成的,所以我可以理解发生了什么?谢谢!

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<sstream>
#include <fstream>
#include <iomanip>
using namespace std;

template <class T>
char *as_bytes(T &i) // treat a T as a sequence of bytes
{
    void *addr = &i; // get the address of the first byte of memory used …
Run Code Online (Sandbox Code Playgroud)

c++ binary-data

2
推荐指数
1
解决办法
198
查看次数

标签 统计

binary-data ×1

c++ ×1