diff options
author | Alex Zolotarev <deathbaba@gmail.com> | 2010-12-05 19:24:16 +0300 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-22 22:33:57 +0300 |
commit | d6e12b7ce4bcbf0ccd1c07eb25de143422913c34 (patch) | |
tree | a7e910c330ce4da9b4f2d8be76067adece2561c4 /coding/hex.cpp |
One Month In Minsk. Made in Belarus.
Diffstat (limited to 'coding/hex.cpp')
-rw-r--r-- | coding/hex.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/coding/hex.cpp b/coding/hex.cpp new file mode 100644 index 0000000000..8043401ce4 --- /dev/null +++ b/coding/hex.cpp @@ -0,0 +1,49 @@ +#include "../base/SRC_FIRST.hpp" +#include "hex.hpp" + +#include "../base/start_mem_debug.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; + } + } +} |