我有一个读取文件的程序,它会读取文件的十六进制并进行修改,最后将修改后的十六进制存储在一个std::string
中。
例如,我如何将这个字符串写入文件?
std::string wut="b6306edf953a6ac8d17d70bda3e93f2a3816eac333d1ac78";
和获取其值
.0n..:j..}p...?*8...3..x
在输出的文件中?
我更愿意不使用 sprintf
,但如果必要的话,我会尽力而为。
我有一个读取文件的程序,它会读取文件的十六进制并进行修改,最后将修改后的十六进制存储在一个std::string
中。
例如,我如何将这个字符串写入文件?
std::string wut="b6306edf953a6ac8d17d70bda3e93f2a3816eac333d1ac78";
.0n..:j..}p...?*8...3..x
在输出的文件中?
我更愿意不使用 sprintf
,但如果必要的话,我会尽力而为。
如果我正确理解你的问题,你想将文本转换为它的数字等价形式,然后写入文件。根据你在问题中提供的提示,似乎应该逐字节地完成这个操作。下面是一种实现方法。请注意,需要将每个字节从字符串转换为整数值。
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <ios>
std::string wut = "b6306edf953a6ac8d17d70bda3e93f2a3816eac333d1ac78";
int main()
{
std::ofstream datafile("c:\\temp\\temp1.dat", std::ios_base::binary | std::ios_base::out);
char buf[3];
buf[2] = 0;
std::stringstream input(wut);
input.flags(std::ios_base::hex);
while (input)
{
input >> buf[0] >> buf[1];
long val = strtol(buf, nullptr, 16);
datafile << static_cast<unsigned char>(val & 0xff);
}
}
@Peter R的答案会导致输出不完全相等,这是由于stringstream
以意外的方式解释了多个连续的“0”。
例如:如果我们想写入十六进制值“00000000”,stringstream
将输出“ 000000”。
下面的解决方案在每种情况下都有效,无论十六进制字符串中包含多少个零:
// Input
std::string hex = "180f00005e2c3415"; // (or longer)
std::basic_string<uint8_t> bytes;
// Iterate over every pair of hex values in the input string (e.g. "18", "0f", ...)
for (size_t i = 0; i < hex.length(); i += 2)
{
uint16_t byte;
// Get current pair and store in nextbyte
std::string nextbyte = hex.substr(i, 2);
// Put the pair into an istringstream and stream it through std::hex for
// conversion into an integer value.
// This will calculate the byte value of your string-represented hex value.
std::istringstream(nextbyte) >> std::hex >> byte;
// As the stream above does not work with uint8 directly,
// we have to cast it now.
// As every pair can have a maximum value of "ff",
// which is "11111111" (8 bits), we will not lose any information during this cast.
// This line adds the current byte value to our final byte "array".
bytes.push_back(static_cast<uint8_t>(byte));
}
// we are now generating a string obj from our bytes-"array"
// this string object contains the non-human-readable binary byte values
// therefore, simply reading it would yield a String like ".0n..:j..}p...?*8...3..x"
// however, this is very useful to output it directly into a binary file like shown below
std::string result(begin(bytes), end(bytes));
std::ofstream output_file("filename", std::ios::binary | std::ios::out);
if (output_file.is_open())
{
output_file << result;
output_file.close();
}
else
{
std::cout << "Error could not create file." << std::endl;
}