2022-02-13 21:37:05 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdarg>
|
2022-03-02 21:10:41 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#if defined(_MSC_VER) && _MSC_VER < 1600
|
|
|
|
#include <basetsd.h>
|
|
|
|
typedef UINT8 uint8_t;
|
2022-03-22 19:19:50 +00:00
|
|
|
typedef UINT16 uint16_t;
|
2022-03-02 21:10:41 +00:00
|
|
|
#else
|
|
|
|
#include <cstdint>
|
|
|
|
#endif
|
2022-02-13 21:37:05 +00:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define ATTRIBUTE_FORMAT_PRINTF(a, b)
|
|
|
|
#else
|
|
|
|
#define ATTRIBUTE_FORMAT_PRINTF(a, b) __attribute__((format(printf, a, b)))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::string StrFormat(const char* format, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
|
|
|
|
std::string StrFormatV(const char* format, va_list va);
|
2022-03-02 21:10:41 +00:00
|
|
|
|
2022-03-22 19:19:50 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char g_aHexDigits[16] = {
|
|
|
|
'0', '1', '2', '3', '4', '5', '6', '7',
|
|
|
|
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
|
|
|
|
};
|
|
|
|
|
|
|
|
// No buffer overflow check or null termination. Use with caution.
|
|
|
|
inline char* StrBufferAppendByteAsHex(char* cp, uint8_t n)
|
|
|
|
{
|
|
|
|
*cp++ = g_aHexDigits[(n >> 4) & 0x0f];
|
|
|
|
*cp++ = g_aHexDigits[(n >> 0) & 0x0f];
|
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No buffer overflow check or null termination. Use with caution.
|
|
|
|
inline char* StrBufferAppendWordAsHex(char* cp, uint16_t n)
|
|
|
|
{
|
|
|
|
*cp++ = g_aHexDigits[(n >> 12) & 0x0f];
|
|
|
|
*cp++ = g_aHexDigits[(n >> 8) & 0x0f];
|
|
|
|
*cp++ = g_aHexDigits[(n >> 4) & 0x0f];
|
|
|
|
*cp++ = g_aHexDigits[(n >> 0) & 0x0f];
|
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
2022-03-02 21:10:41 +00:00
|
|
|
inline std::string& StrAppendByteAsHex(std::string& s, uint8_t n)
|
|
|
|
{
|
2022-03-22 19:19:50 +00:00
|
|
|
const char hex[2] = { g_aHexDigits[(n >> 4) & 0x0f],
|
|
|
|
g_aHexDigits[(n >> 0) & 0x0f] };
|
|
|
|
return s.append(hex, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string& StrAppendWordAsHex(std::string& s, uint16_t n)
|
|
|
|
{
|
|
|
|
const char hex[4] = { g_aHexDigits[(n >> 12) & 0x0f],
|
|
|
|
g_aHexDigits[(n >> 8) & 0x0f],
|
|
|
|
g_aHexDigits[(n >> 4) & 0x0f],
|
|
|
|
g_aHexDigits[(n >> 0) & 0x0f] };
|
|
|
|
return s.append(hex, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string ByteToHexStr(uint8_t n)
|
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
StrAppendByteAsHex(s, n);
|
2022-03-02 21:10:41 +00:00
|
|
|
return s;
|
|
|
|
}
|
2022-03-22 19:19:50 +00:00
|
|
|
|
|
|
|
inline std::string WordToHexStr(uint16_t n)
|
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
StrAppendWordAsHex(s, n);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2022-10-02 15:28:03 +00:00
|
|
|
inline std::string DWordToHexStr(uint32_t n)
|
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
StrAppendWordAsHex(s, n >> 16);
|
|
|
|
StrAppendWordAsHex(s, n);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2022-03-22 19:19:50 +00:00
|
|
|
} // namespace
|