| 463 | } |
| 464 | |
| 465 | boost::dynamic_bitset<> TokenFunc::asBinary(const Token& token) { |
| 466 | const std::string& str = asStringRef(token); |
| 467 | if (str.empty()) { |
| 468 | throw IfcException("Token is not a valid binary sequence"); |
| 469 | } |
| 470 | |
| 471 | std::string::const_iterator it = str.begin(); |
| 472 | int n = *it - '0'; |
| 473 | if ((n < 0 || n > 3) || (str.size() == 1 && n != 0)) { |
| 474 | throw IfcException("Token is not a valid binary sequence"); |
| 475 | } |
| 476 | |
| 477 | ++it; |
| 478 | unsigned i = ((unsigned)str.size() - 1) * 4 - n; |
| 479 | boost::dynamic_bitset<> bitset(i); |
| 480 | |
| 481 | for (; it != str.end(); ++it) { |
| 482 | const std::string::value_type& c = *it; |
| 483 | int value = (c < 'A') ? (c - '0') : (c - 'A' + 10); |
| 484 | for (unsigned j = 0; j < 4; ++j) { |
| 485 | if (i-- == 0) { |
| 486 | break; |
| 487 | } |
| 488 | if ((value & (1 << (3 - j))) != 0) { |
| 489 | bitset.set(i); |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | return bitset; |
| 495 | } |
| 496 | |
| 497 | std::string TokenFunc::toString(const Token& token) { |
| 498 | std::string result; |