/****************************************************************************** Online C++ Compiler. Code, Compile, Run and Debug C++ program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include #include #include #include using namespace std; constexpr int8_t kHexDigits[256] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, }; inline int8_t HexToInt(char c) { return kHexDigits[uint8_t(c)]; } inline bool IsValidHex(std::string s) { return std::all_of(s.begin(), s.end(), [](char c) { return HexToInt(c) != -1; }); } /** * Converts a hexadecimal to binary format if the hex string will fit the buffer. * Smaller hex strings are left padded with zeroes. */ inline bool HexToBinary(std::string hex, uint8_t *buffer, size_t buffer_size) { memset(buffer, 0, buffer_size); if (hex.size() > buffer_size * 2) { return false; } int64_t hex_size = int64_t(hex.size()); int64_t buffer_pos = int64_t(buffer_size) - (hex_size + 1) / 2; int64_t last_hex_pos = hex_size - 1; int64_t i = 0; for (; i < last_hex_pos; i += 2) { buffer[buffer_pos++] = (HexToInt(hex[i]) << 4) | HexToInt(hex[i + 1]); } if (i == last_hex_pos) { buffer[buffer_pos] = HexToInt(hex[i]); } return true; } inline bool HexToBinaryFixed(std::string hex, uint8_t *buffer, size_t buffer_size) { memset(buffer, 0, buffer_size); if (hex.size() > buffer_size * 2) { return false; } int64_t hex_size = int64_t(hex.size()); int64_t buffer_pos = int64_t(buffer_size) - (hex_size + 1) / 2; int64_t last_hex_pos = hex_size - 1; bool is_hex_size_odd = (hex_size % 2) == 1; int64_t i = 0; if (is_hex_size_odd) { buffer[buffer_pos++] = HexToInt(hex[i++]); } for (; i < last_hex_pos; i += 2) { buffer[buffer_pos++] = (HexToInt(hex[i]) << 4) | HexToInt(hex[i + 1]); } return true; } int main() { std::string trace_id_hex = "78cfcfec62ae9e9"; uint8_t trace_id[16]; HexToBinary(trace_id_hex, trace_id, sizeof(trace_id)); uint8_t trace_id_fix[16]; HexToBinaryFixed(trace_id_hex, trace_id_fix, sizeof(trace_id)); cout<<"Hello World"; return 0; }