hexxer.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132
  1. // from https://github.com/dcousens/hexxer/blob/47191b839fc4bbdc60dc267d9f9673640a50c161/hexxer.hpp
  2. #pragma once
  3. namespace hexxer {
  4. static const char HEX_ALPHABET[] = "0123456789abcdef";
  5. static const int HEX_TABLE[] = {
  6. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  7. 0,1,2,3,4,5,6,7,8,9, // 0-9
  8. 255,255,255,255,255,255,255,
  9. 10,11,12,13,14,15, // a-f
  10. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  11. 10,11,12,13,14,15, // A-F
  12. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
  13. };
  14. inline auto encodeFirst (const unsigned char x) {
  15. return HEX_ALPHABET[x >> 4];
  16. }
  17. inline auto encodeSecond (const unsigned char x) {
  18. return HEX_ALPHABET[x & 0x0f];
  19. }
  20. inline auto decode (const char a, const char b) {
  21. const auto ia = HEX_TABLE[static_cast<unsigned char>(a)];
  22. const auto ib = HEX_TABLE[static_cast<unsigned char>(b)];
  23. if (ia == 255) return 0x100;
  24. if (ib == 255) return 0x100;
  25. return (ia << 4) + ib;
  26. }
  27. }