以十六进制读取/写入二进制对象
Read/write binary object as hex?
我需要将各种结构序列化到一个文件中。如果可能的话,我希望这些文件是纯ASCII。我可以为每个结构编写某种序列化程序,但有成百上千的结构包含float
和double
,我想准确地表示它们。
我不能使用第三方序列化库,也没有时间编写数百个序列化程序。
如何使用ASCII安全序列化此数据?也请流,我讨厌C风格的printf("%02x",data)
的外观。
我在网上找到了这个解决方案,它正好解决了这个问题:
https://jdale88.wordpress.com/2009/09/24/c-anything-tofrom-a-hex-string/
转载如下:
#include <string>
#include <sstream>
#include <iomanip>
// ------------------------------------------------------------------
/*!
Convert a block of data to a hex string
*/
void toHex(
void *const data, //!< Data to convert
const size_t dataLength, //!< Length of the data to convert
std::string &dest //!< Destination string
)
{
unsigned char *byteData = reinterpret_cast<unsigned char*>(data);
std::stringstream hexStringStream;
hexStringStream << std::hex << std::setfill('0');
for(size_t index = 0; index < dataLength; ++index)
hexStringStream << std::setw(2) << static_cast<int>(byteData[index]);
dest = hexStringStream.str();
}
// ------------------------------------------------------------------
/*!
Convert a hex string to a block of data
*/
void fromHex(
const std::string &in, //!< Input hex string
void *const data //!< Data store
)
{
size_t length = in.length();
unsigned char *byteData = reinterpret_cast<unsigned char*>(data);
std::stringstream hexStringStream; hexStringStream >> std::hex;
for(size_t strIndex = 0, dataIndex = 0; strIndex < length; ++dataIndex)
{
// Read out and convert the string two characters at a time
const char tmpStr[3] = { in[strIndex++], in[strIndex++], 0 };
// Reset and fill the string stream
hexStringStream.clear();
hexStringStream.str(tmpStr);
// Do the conversion
int tmpValue = 0;
hexStringStream >> tmpValue;
byteData[dataIndex] = static_cast<unsigned char>(tmpValue);
}
}
这可以很容易地适用于对文件流的读/写,尽管fromHex
中使用的stringstream
仍然是必要的,但转换必须一次完成两个读取字符。
无论采用何种方式,都需要每个结构类型。不能只将结构位复制到外部世界,并期望它发挥作用。
如果你想要纯ascii,不要用hex。对于序列化float
和double
,将输出流设置为科学,float
的精度为8double
。(这将需要更多的字节,但实际上工作。)
对于其余部分:如果结构是干净地编写的,则根据一些内部编程指南,并且仅包含基本类型,您应该能够直接解析它们。否则最简单的解决方案通常是设计一个非常简单的描述符语言,描述其中的每个结构,并运行代码生成器来获取序列化代码。
相关文章:
- 如何将二进制格式的 C++ 对象的 std::vector 保存到磁盘?
- 对在不同二进制文件中创建的对象文件的依赖关系
- 如何将包含另一个对象向量的对象保存到文件中,并使用C++中的二进制文件从文件中读回?
- 使用此指针在 c++ 中将对象写入二进制文件
- 无法使用迭代器查找类对象。(二进制 '==': 'userInfo' 不定义此运算符)
- 将对象信息保存到二进制文件中
- 使用二进制搜索树中的迭代器对象访问左侧节点
- 如何使用自定义对象的序列化在 c++ 中编写自定义二进制文件处理程序
- C#将类对象写入二进制文件
- C++/Python 中对象和结构的二进制文件结构
- 在C 中对二进制对象进行编码/解码的标准方法
- 二进制"+=":尝试将对象添加到对象数组时未找到全局运算符
- 对大于属性的元素的矢量对象进行二进制搜索
- 可以通过跨二进制边界传递的对象上的接口调用虚拟函数吗?
- 将对象(指针)在地图中输出到二进制文件
- 未显式引用对象的全局对象构造函数在最终二进制文件 - LD 中被丢弃
- 使用 C++ 将对象读取和写入二进制文件
- 在二进制搜索树中对对象值排序
- 对象分配对二进制大小的影响是什么
- 以十六进制读取/写入二进制对象