qi::xdigit does not do what you think it does: returns a raw character (i.e. '0' , not 0x00 ).
You can use qi::uint_parser to your advantage, making your syntax much easier as a bonus:
typedef qi::uint_parser<uchar, 16, 2, 2> xuchar;
- No need to rely on phoenix (to work on older versions of Boost)
- get both characters at a time (otherwise you may need to add multiple castings to prevent the extension of entire extensions)
Here is a copied sample:
#include <iostream> #include <string> #define BOOST_SPIRIT_UNICODE #include <boost/cstdint.hpp> #include <boost/spirit/include/qi.hpp> typedef boost::uint32_t uchar; // Unicode codepoint namespace qi = boost::spirit::qi; typedef qi::uint_parser<uchar, 16, 2, 2> xuchar; const static xuchar xuchar_ = xuchar(); int main(int argc, char **argv) { // Input std::string input = "%3C"; std::string::const_iterator begin = input.begin(); std::string::const_iterator end = input.end(); qi::rule<std::string::const_iterator, uchar()> pchar = '%' > xuchar_; uchar result; bool r = qi::parse(begin, end, pchar, result); if (r && begin == end) { std::cout << "Output: " << result << std::endl; std::cout << "Expected: < (LESS-THAN SIGN)" << std::endl; } else { std::cerr << "Error" << std::endl; return 1; } return 0; }
Output:
Output: 60 Expected: < (LESS-THAN SIGN)
'<' is really ASCII 60
source share