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: b82214d2fda855fc1a04085319c22dcc622fa23f (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
49
#include "coding/hex.hpp"

#include "base/assert.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;
    }
  }

  uint8_t HexDigitToRaw(uint8_t const digit)
  {
    if (digit >= '0' && digit <= '9')
      return (digit - '0');
    else if (digit >= 'A' && digit <= 'F')
      return (digit - 'A' + 10);
    else if (digit >= 'a' && digit <= 'f')
      return (digit - 'a' + 10);
    ASSERT(false, (digit));
    return 0;
  }

  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 = HexDigitToRaw(*ptr++) << 4;
      *out |= HexDigitToRaw(*ptr++);
      ++out;
    }
  }
}