最近,我尝试使用boost :: spirit :: qi二进制字节序解析器来解析一些二进制数据取决于平台的字节顺序.有一个简单的例子,如下所示:
使用声明和变量:
using boost::spirit::qi::little_word; using boost::spirit::qi::little_dword; using boost::spirit::qi::little_qword; boost::uint16_t us; boost::uint32_t ui; boost::uint64_t ul;
little endian二进制解析器的基本用法:
test_parser_attr("\x01\x02", little_word, us); assert(us == 0x0201);
test_parser_attr("\x01\x02\x03\x04", little_dword, ui); assert(ui == 0x04030201);
test_parser_attr("\x01\x02\x03\x04\x05\x06\x07\x08", little_qword, ul);
assert(ul == 0x0807060504030201LL);
test_parser("\x01\x02", little_word(0x0201));
test_parser("\x01\x02\x03\x04", little_dword(0x04030201));
test_parser("\x01\x02\x03\x04\x05\x06\x07\x08",
little_qword(0x0807060504030201LL));
Run Code Online (Sandbox Code Playgroud)
它工作得很好.但我的问题来了,为什么我们需要使用一些数据类型,例如boost::uint16_t,boost::uint32_t在这里?我可以使用unsigned long或unsigned int在这里?如果我想解析double或float数据类型,我应该使用什么样的boost数据类型?请告诉我boost在哪里定义以上这些类型?