나는 바이너리 엔디안 파서를 사용하는 방법을 배우고있다. here 및 basics examples에 따라 작은 테스트 파서 프로그램을 작성하지만 올바르게 작동하지 않습니다. 그것은 나에게 msg를 주었다 : "Error : no match".부스트 바이너리 엔디안 파서가 작동하지 않습니까?
여기 내 코드입니다.
#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 컴파일러를 사용합니다.
감사합니다. 나는 내가 기대하는 것을 얻지 못했다. 그래서 내가 unsigned 32 비트 int로 먼저 구문 분석해야합니다, 그런 다음 static_cast float로? 아니면이 일을 끝내기위한 더 좋은 방법이 있습니까? 바이트를 float 또는 double로 직접 스왑해야합니까? –
일반적으로 (IEEE-754에서) float 및 double은 엔디안에 민감하지 않으며, 모든 아키텍처에서 레이아웃이 동일합니다. float와 double의 이진 (비트) 표현을 Spirit을 사용하여 입/출력해야하는 경우 다음과 같이 추악한 것을해야 할 것입니다 : float f; * reinterpret_cast (&f); 또는 일부 불쾌한 속임수와 조합입니다.하지만 지금 생각해 보면 가장 좋은 해결책은 bin_float 및 bin_double 파서/생성기를 Spirit에 추가하는 것입니다. 현재 실종 상태입니다. 이것에 관한 성령 웹 사이트에 ... –
hkaiser
맞아요. 나는 스피릿 번호를 다루기 위해 스피릿이 필요하지 않습니다. 여기에서 제 대답을 찾았습니다. http://stackoverflow.com/questions/1695681/reading-from-and- 쓰기 -이 - 중간 - 중 - 중 - 바이너리 - 파일 -에서 - cc –