所以我知道这个问题已经被问过很多次了.很抱歉再次提出要求,但我没有发现关于我的具体情况的任何疑问.
So I know this question has been asked many times. I'm sorry for asking again, but I haven't found any questions asked before with my specific circumstances.
因此,我有一个程序可以读取文件的十六进制,对其进行修改,然后将修改后的十六进制存储在std :: string中. 因此,例如,我如何将其写入文件
So I have a program that reads the hex of a file, modifies it, and stores the modified hex in an std::string. So for example, how would I write this to a file
std::string wut="b6306edf953a6ac8d17d70bda3e93f2a3816eac333d1ac78";并获得其价值
.0n..:j..}p...?*8...3..x在输出的文件中?
我不想使用sprintf,但我想如果有必要,我会做我必须做的事.
I'd prefer not to use sprintf, but I guess if it's necessary, I'll do what I must.
谢谢大家, 〜P
推荐答案如果我正确理解了您的问题,则希望将文本转换为等效的数字,然后再写入文件.根据您在问题中提供的提示,看起来应该逐个字节地完成.下面是实现此目的的一种方法.请注意,需要将每个字节从字符串转换为整数值.
If I understand your question correctly you want the text converted to it's numeric equivalent and then written to file. Given the hint you provided in your question it looks like this should be done byte by byte. Below is one way to achieve this. Note the need to convert each byte from a string to an integer value.
#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); } }更多推荐
将十六进制写入文件C ++
发布评论