Welcome to mirror list, hosted at ThFree Co, Russian Federation.

hex.cpp « coding - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6db94166de07f6d45a7f39e0b44c3d6a315b9e2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "../base/SRC_FIRST.hpp"
#include "hex.hpp"


namespace impl {
  static const char kToHexTable[] = "0123456789ABCDEF";

  void ToHexRaw(void const * src, size_t size, void * dst)
  {
    uint8_t const * ptr = static_cast<uint8_t const *>(src);
    uint8_t const * end = ptr + size;
    uint8_t * out = static_cast<uint8_t*>(dst);

    while (ptr != end)
    {
      *out++ = kToHexTable[(*ptr) >> 4];
      *out++ = kToHexTable[(*ptr) & 0xF];
      ++ptr;
    }
  }

//  static const char kFromHexTable[] = "0123456789ABCDEF";

  void FromHexRaw(void const * src, size_t size, void * dst)
  {
    uint8_t const * ptr = static_cast<uint8_t const *>(src);
    uint8_t const * end = ptr + size;
    uint8_t * out = static_cast<uint8_t*>(dst);

    while (ptr < end) {
      *out = 0;
      if (*ptr >= '0' && *ptr <= '9') {
        *out |= ((*ptr - '0') << 4);
      } else if (*ptr >= 'A' && *ptr <= 'F') {
        *out |= ((*ptr - 'A' + 10) << 4);
      }
      ++ptr;

      if (*ptr >= '0' && *ptr <= '9') {
        *out |= ((*ptr - '0') & 0xF);
      } else if (*ptr >= 'A' && *ptr <= 'F') {
        *out |= ((*ptr - 'A' + 10) & 0xF);
      }
      ++ptr;
      ++out;
    }
  }
}