[英]Boost Binary Endian parser not working?
我正在研究如何使用增強精神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編譯器。
big_word
解析器匹配一個big-endian字(16位),而big_dword
則匹配您想要的內容(big-endian dword,32位)。 但是我認為使用浮點數作為二進制解析器的屬性不是一個好主意,您可能無法獲得期望的結果。
我使用了錯誤的解析參數。 我不應該使用big_word
而不是big_dword
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.