std :: bitset的二进制序列化 [英] Binary Serialization of std::bitset

查看:263
本文介绍了std :: bitset的二进制序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std :: bitset 有一个 to_string()方法,用于序列化为 1 s和 0 的字符串显然,这对bitset中的每个位使用单个8位 char ,使得序列化表示比必要的长8倍。

我想以二进制表示存储bitset以节省空间。 to_ulong()方法仅在我的bitset中少于32位时才是相关的。我有数百个。

我不确定我想使用 memcpy() / std :: copy()对象(地址)本身,因为假定对象是POD。

std::bitset has a to_string() method for serializing as a char-based string of 1s and 0s. Obviously, this uses a single 8 bit char for each bit in the bitset, making the serialized representation 8 times longer than necessary.
I want to store the bitset in a binary representation to save space. The to_ulong() method is relevant only when there are less than 32 bits in my bitset. I have hundreds.
I'm not sure I want to use memcpy()/std::copy() on the object (address) itself, as that assumes the object is a POD.

API似乎不提供内部的句柄

The API does not seem to provide a handle to the internal array representation from which I could have taken the address.

我也喜欢从二进制表示中反序列化bitset的选项。

I would also like the option to deserialize the bitset from the binary representation.

我该如何做?

推荐答案

这是一种可能的方法, c $ c> std :: vector< unsigned char> 每次读/写一个位...

This is a possible approach based on explicit creation of an std::vector<unsigned char> by reading/writing one bit at a time...

template<size_t N>
std::vector<unsigned char> bitset_to_bytes(const std::bitset<N>& bs)
{
    std::vector<unsigned char> result((N + 7) >> 3);
    for (int j=0; j<int(N); j++)
        result[j>>3] |= (bs[j] << (j & 7));
    return result;
}

template<size_t N>
std::bitset<N> bitset_from_bytes(const std::vector<unsigned char>& buf)
{
    assert(buf.size() == ((N + 7) >> 3));
    std::bitset<N> result;
    for (int j=0; j<int(N); j++)
        result[j] = ((buf[j>>3] >> (j & 7)) & 1);
    return result;
}


$ b < bitset_from_bytes 必须在函数调用中指定位集大小 N ,例如

Note that to call the de-serialization template function bitset_from_bytes the bitset size N must be specified in the function call, for example

std::bitset<N> bs1;
...
std::vector<unsigned char> buffer = bitset_to_bytes(bs1);
...
std::bitset<N> bs2 = bitset_from_bytes<N>(buffer);

如果你真的关心速度一个解决方案,会得到一些东西会做一个循环展开,使打包是一次完成一个字节,但更好的是只写自己的bitset实现,不隐藏内部二进制表示,而不是使用 std :: bitset

If you really care about speed one solution that would gain something would be doing a loop unrolling so that the packing is done for example one byte at a time, but even better is just to write your own bitset implementation that doesn't hide the internal binary representation instead of using std::bitset.

这篇关于std :: bitset的二进制序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆