c++ - Boost Binary Endian 解析器不工作?

标签 c++ binary endianness boost-spirit boost-spirit-qi

我正在研究如何使用boost spirit Qi binary endian parser。我根据 here 写了一个小测试解析器程序和 basics examples , 但它不能正常工作。它给了我消息:“错误:不匹配”。

这是我的代码。

 
    #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<p></p>

   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 编译器。

最佳答案

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

关于c++ - Boost Binary Endian 解析器不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2870005/

相关文章:

c++ - 表达式无法准备在目标中运行

c++ - 如果返回,lambda 会超出范围吗

c - 转到 C 中二进制文件的某个点(使用 fseek),然后从该位置读取(使用 fread)

list - Prolog作业: how to parse a list of 'bits' representing a numeric value into a decimal representation?

c++ - 为什么我的值被位域除以 2

c++ - C++检测Windows操作系统

c++ - 优化查找复数作为输入

binary - 如何将分数转换为二进制?

c - 严格可移植 C 中浮点字节顺序的高效转换

c - 演示长整数的字节顺序