操作方法:将 boost::endian 缓冲区类型转换回本机格式



buffers.hpp 的 boost::endian 示例展示了如何将本机格式转换为大端或小端序。如何执行反函数以使大/小端格式恢复为本机格式?

例:

#include <stdint.h>
#include <boost/endian/buffers.hpp>
using namespace boost::endian;
int main() {
uint64_t v= uint64_t(0x1011121314151617);
big_uint64_buf_t    b;
little_uint64_buf_t l;
// Set values, implicit native_to_*
b= v;
l= v;
// Get values, does not work
v= b;
v= l;
return 0;
}

gcc编译器抱怨:

示例.cpp:17:6:错误:无法在赋值中将"Boost::Endian::big_uint64_buf_t {aka boost::endian::endian_buffer<(boost::endian::order)0, long unsigned int, 64ul>}' 转换为 'uint64_t {aka long unsigned int}'

 v= b;
  ^

从小端格式转换回来时会发生类似的错误。

使用:

v= b.value(); // and/or
v= l.value();

尽管示例中缺少值函数用法,但它记录在成员函数列表中。

最新更新