以十六进制读取/写入二进制对象

Read/write binary object as hex?

本文关键字:二进制 对象 十六进制 读取      更新时间:2023-10-16

我需要将各种结构序列化到一个文件中。如果可能的话,我希望这些文件是纯ASCII。我可以为每个结构编写某种序列化程序,但有成百上千的结构包含floatdouble,我想准确地表示它们。

我不能使用第三方序列化库,也没有时间编写数百个序列化程序。

如何使用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。对于序列化floatdouble,将输出流设置为科学,float的精度为8double。(这将需要更多的字节,但实际上工作。)

对于其余部分:如果结构是干净地编写的,则根据一些内部编程指南,并且包含基本类型,您应该能够直接解析它们。否则最简单的解决方案通常是设计一个非常简单的描述符语言,描述其中的每个结构,并运行代码生成器来获取序列化代码。