问题描述
编程原理与实践在第11章中说: “在内存中,我们可以将数字123表示为整数值(每个int表示4个字节)或表示字符串值(每个字符表示1个字节)”。
在读取二进制文本文件时,我试图了解存储在存储器中的内容。 所以我正在写向量v的内容。
如果输入文件包含以下文本:“测试这些单词”
输出文件显示以下数字:1953719668 1701344288 1998611827 1935962735 168626701 168626701 168626701 168626701 168626701 168626701
我试图将“测试”的每个字符转换为二进制 和我有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 to store the object
return static_cast<char *>(addr); // treat that memory as bytes
}
int main()
{
string iname{"11.9_in.txt"};
ifstream ifs {iname,ios_base::binary}; // note: stream mode
string oname{"11.9_out.txt"};
ofstream ofs {oname,ios_base::binary}; // note: stream mode
vector<int> v;
// read from binary file:
for(int x; ifs.read(as_bytes(x),sizeof(int)); ) // note: reading bytes
v.push_back(x);
for(int x : v)
ofs << x << ' ';
}
解决方法
让我假设您使用的是Little-endian机器(例如x86)和ASCII兼容的字符代码(例如Shift_JIS和UTF-8)。
74 65 73 74
表示为0x74736574
的二进制数据。
使用little-endian,将muitl-byte整数的较高字节放置到较高地址。
因此,将thes读取为4字节整数,将被解释为1953719668
,十进制为document.data(as: )
。