2016-12-11 13:55:13 +00:00
|
|
|
#pragma once
|
2018-08-21 15:56:50 +00:00
|
|
|
#include <string>
|
2016-12-11 13:55:13 +00:00
|
|
|
|
2017-06-01 13:41:58 +00:00
|
|
|
|
|
|
|
/// Maps 0..15 to 0..9A..F or 0..9a..f correspondingly.
|
|
|
|
|
|
|
|
extern const char * const hex_digit_to_char_uppercase_table;
|
|
|
|
extern const char * const hex_digit_to_char_lowercase_table;
|
|
|
|
|
2017-07-23 06:53:28 +00:00
|
|
|
inline char hexDigitUppercase(unsigned char c)
|
2017-06-01 13:41:58 +00:00
|
|
|
{
|
|
|
|
return hex_digit_to_char_uppercase_table[c];
|
|
|
|
}
|
|
|
|
|
2017-07-23 06:53:28 +00:00
|
|
|
inline char hexDigitLowercase(unsigned char c)
|
2017-06-01 13:41:58 +00:00
|
|
|
{
|
|
|
|
return hex_digit_to_char_lowercase_table[c];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-23 06:53:28 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <cstddef>
|
|
|
|
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/types.h>
|
2017-08-09 01:34:01 +00:00
|
|
|
|
|
|
|
|
2017-07-23 06:53:28 +00:00
|
|
|
/// Maps 0..255 to 00..FF or 00..ff correspondingly
|
|
|
|
|
|
|
|
extern const char * const hex_byte_to_char_uppercase_table;
|
|
|
|
extern const char * const hex_byte_to_char_lowercase_table;
|
|
|
|
|
2017-08-09 01:34:01 +00:00
|
|
|
inline void writeHexByteUppercase(UInt8 byte, void * out)
|
2017-07-23 06:53:28 +00:00
|
|
|
{
|
|
|
|
memcpy(out, &hex_byte_to_char_uppercase_table[static_cast<size_t>(byte) * 2], 2);
|
|
|
|
}
|
|
|
|
|
2017-08-09 01:34:01 +00:00
|
|
|
inline void writeHexByteLowercase(UInt8 byte, void * out)
|
2017-07-23 06:53:28 +00:00
|
|
|
{
|
|
|
|
memcpy(out, &hex_byte_to_char_lowercase_table[static_cast<size_t>(byte) * 2], 2);
|
|
|
|
}
|
|
|
|
|
2021-06-24 11:35:19 +00:00
|
|
|
extern const char * const bin_byte_to_char_table;
|
|
|
|
|
|
|
|
inline void writeBinByte(UInt8 byte, void * out)
|
|
|
|
{
|
|
|
|
memcpy(out, &bin_byte_to_char_table[static_cast<size_t>(byte) * 8], 8);
|
|
|
|
}
|
|
|
|
|
2018-05-21 13:49:54 +00:00
|
|
|
/// Produces hex representation of an unsigned int with leading zeros (for checksums)
|
|
|
|
template <typename TUInt>
|
2019-08-03 11:02:40 +00:00
|
|
|
inline void writeHexUIntImpl(TUInt uint_, char * out, const char * const table)
|
2018-05-21 13:49:54 +00:00
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
TUInt value;
|
|
|
|
UInt8 uint8[sizeof(TUInt)];
|
|
|
|
};
|
|
|
|
|
2019-08-03 11:02:40 +00:00
|
|
|
value = uint_;
|
2018-05-21 13:49:54 +00:00
|
|
|
|
|
|
|
/// Use little endian
|
|
|
|
for (size_t i = 0; i < sizeof(TUInt); ++i)
|
|
|
|
memcpy(out + i * 2, &table[static_cast<size_t>(uint8[sizeof(TUInt) - 1 - i]) * 2], 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename TUInt>
|
2019-08-03 11:02:40 +00:00
|
|
|
inline void writeHexUIntUppercase(TUInt uint_, char * out)
|
2018-05-21 13:49:54 +00:00
|
|
|
{
|
2019-08-03 11:02:40 +00:00
|
|
|
writeHexUIntImpl(uint_, out, hex_byte_to_char_uppercase_table);
|
2018-05-21 13:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename TUInt>
|
2019-08-03 11:02:40 +00:00
|
|
|
inline void writeHexUIntLowercase(TUInt uint_, char * out)
|
2018-05-21 13:49:54 +00:00
|
|
|
{
|
2019-08-03 11:02:40 +00:00
|
|
|
writeHexUIntImpl(uint_, out, hex_byte_to_char_lowercase_table);
|
2018-05-21 13:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename TUInt>
|
2019-08-03 11:02:40 +00:00
|
|
|
std::string getHexUIntUppercase(TUInt uint_)
|
2018-05-21 13:49:54 +00:00
|
|
|
{
|
|
|
|
std::string res(sizeof(TUInt) * 2, '\0');
|
2019-08-03 11:02:40 +00:00
|
|
|
writeHexUIntUppercase(uint_, res.data());
|
2018-05-21 13:49:54 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename TUInt>
|
2019-08-03 11:02:40 +00:00
|
|
|
std::string getHexUIntLowercase(TUInt uint_)
|
2018-05-21 13:49:54 +00:00
|
|
|
{
|
|
|
|
std::string res(sizeof(TUInt) * 2, '\0');
|
2019-08-03 11:02:40 +00:00
|
|
|
writeHexUIntLowercase(uint_, res.data());
|
2018-05-21 13:49:54 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-01 13:41:58 +00:00
|
|
|
/// Maps 0..9, A..F, a..f to 0..15. Other chars are mapped to implementation specific value.
|
|
|
|
|
|
|
|
extern const char * const hex_char_to_digit_table;
|
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
inline UInt8 unhex(char c)
|
2017-06-01 13:41:58 +00:00
|
|
|
{
|
2017-08-09 01:34:01 +00:00
|
|
|
return hex_char_to_digit_table[static_cast<UInt8>(c)];
|
|
|
|
}
|
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
inline UInt8 unhex2(const char * data)
|
2017-08-09 01:34:01 +00:00
|
|
|
{
|
|
|
|
return
|
|
|
|
static_cast<UInt8>(unhex(data[0])) * 0x10
|
|
|
|
+ static_cast<UInt8>(unhex(data[1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline UInt16 unhex4(const char * data)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
static_cast<UInt16>(unhex(data[0])) * 0x1000
|
|
|
|
+ static_cast<UInt16>(unhex(data[1])) * 0x100
|
|
|
|
+ static_cast<UInt16>(unhex(data[2])) * 0x10
|
|
|
|
+ static_cast<UInt16>(unhex(data[3]));
|
2017-06-01 13:41:58 +00:00
|
|
|
}
|
2021-11-06 08:18:21 +00:00
|
|
|
|
|
|
|
template <typename TUInt>
|
|
|
|
TUInt unhexUInt(const char * data)
|
|
|
|
{
|
|
|
|
TUInt res = 0;
|
|
|
|
if constexpr ((sizeof(TUInt) <= 8) || ((sizeof(TUInt) % 8) != 0))
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < sizeof(TUInt) * 2; ++i, ++data)
|
|
|
|
{
|
|
|
|
res <<= 4;
|
|
|
|
res += unhex(*data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < sizeof(TUInt) / 8; ++i, data += 16)
|
|
|
|
{
|
|
|
|
res <<= 64;
|
|
|
|
res += unhexUInt<UInt64>(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|