Boost Binary Endian 解析器不工作?

发布于 2024-09-02 07:58:54 字数 1838 浏览 7 评论 0原文

我正在研究如何使用 boostspirit Qi 二进制字节序解析器。我根据 此处基本示例,但无法正常工作。它给了我消息:“错误:不匹配”。

这是我的代码。

 
    #include "boost/spirit/include/qi.hpp"
    #include "boost/spirit/include/phoenix_core.hpp"
    #include "boost/spirit/include/phoenix_operator.hpp"
    #include "boost/spirit/include/qi_binary.hpp"  // parsing binary data in various endianness

template "<"typename P, typename T> void binary_parser( char const* input, P const& endian_word_type, T& voxel, bool full_match = true) { using boost::spirit::qi::parse; char const* f(input); char const* l(f + strlen(f)); bool result1 = parse(f,l,endian_word_type,voxel); bool result2 =((!full_match) || (f ==l)); if ( result1 && result2) { //doing nothing, parsing data is pass to voxel alreay } else { std::cerr << "Error: not match!!" << std::endl; exit(1); } } typedef boost::uint16_t bs_int16; typedef boost::uint32_t bs_int32; int main ( int argc, char *argv[] ){ namespace qi = boost::spirit::qi; namespace ascii = boost::spirit::ascii; using qi::big_word; using qi::big_dword; boost::uint32_t ui; float uf; binary_parser("\x01\x02\x03\x04",big_word,ui); assert(ui=0x01020304); binary_parser("\x01\x02\x03\x04",big_word,uf); assert(uf=0x01020304); return 0; }

我几乎复制了这个例子,但为什么这个二进制解析器不起作用。我使用 Mac OS 10.5.8 和 gcc 4.01 编译器。

I am studying how to use boost spirit Qi binary endian parser. I write a small test parser program according to here and basics examples, but it doesn't work proper. It gave me the msg:"Error:no match".

Here is my code.

 
    #include "boost/spirit/include/qi.hpp"
    #include "boost/spirit/include/phoenix_core.hpp"
    #include "boost/spirit/include/phoenix_operator.hpp"
    #include "boost/spirit/include/qi_binary.hpp"  // parsing binary data in various endianness

template "<"typename P, typename T> void binary_parser( char const* input, P const& endian_word_type, T& voxel, bool full_match = true) { using boost::spirit::qi::parse; char const* f(input); char const* l(f + strlen(f)); bool result1 = parse(f,l,endian_word_type,voxel); bool result2 =((!full_match) || (f ==l)); if ( result1 && result2) { //doing nothing, parsing data is pass to voxel alreay } else { std::cerr << "Error: not match!!" << std::endl; exit(1); } } typedef boost::uint16_t bs_int16; typedef boost::uint32_t bs_int32; int main ( int argc, char *argv[] ){ namespace qi = boost::spirit::qi; namespace ascii = boost::spirit::ascii; using qi::big_word; using qi::big_dword; boost::uint32_t ui; float uf; binary_parser("\x01\x02\x03\x04",big_word,ui); assert(ui=0x01020304); binary_parser("\x01\x02\x03\x04",big_word,uf); assert(uf=0x01020304); return 0; }

I almost copy the example, but why this binary parser doesn't work. I use Mac OS 10.5.8 and gcc 4.01 compiler.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹沉浮 2024-09-09 07:58:54

big_word 解析器匹配大端字(16 位),而 big_dword 将匹配您想要的内容(大端双字,32 位)。但我认为使用浮点数作为二进制解析器的属性不是一个好主意,您可能不会得到您所期望的结果。

The big_word parser matches a big-endian word (16 bits), while big_dword will match what you want (big-endian dword, 32 bits). But I don't think it to be a good idea to use a float as the attribute to the binary parser, you might not get what you expect.

捶死心动 2024-09-09 07:58:54

我使用了错误的解析参数。我不应该使用 big_word 而不是 big_dword

I used wrong parse param. I should not use big_word instead of big_dword

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文