C++ int32, int64 和十六进制字符串的转换
#include <iostream> #include <string> #include <cstring> // 用于 memset, strlen #include <algorithm> /** * @brife: 将一个 int64 数字转为 十六进制字符串 * @note: int64Value: 0, hexStr:0000000000000000 int64Value: -1, hexStr:ffffffffffffffff int64Value: 1, hexStr:0000000000000001 int64Value: 1, hexStr:0000000000000001 int64Value: 12, hexStr:000000000000000c int64Value: 123, hexStr:000000000000007b int64Value: 1234, hexStr:00000000000004d2 int64Value: 12345, hexStr:0000000000003039 int64Value: -8056929837760094207, hexStr:9030090000106001 int64Value: 1166442199094681601, hexStr:1030090000106001 */ std::string Int64ToHex(int64_t value) { int BYTE_LEN = 16; // int64_t 占 16 字节,可以表示 16 个 十六进制字符 // 处理负数的一种方式是将其视为无符号数的补码 // 注意:这里我们直接以无符号方式处理,因为十六进制通常不直接表示符号 uint64_t uvalue = static_cast<uint64_t>(value); // 创建一个足够大的字符数组来存储结果(包括终止符'\0') // 64位整数最多需要16个十六进制数字加上终止符 char hexStr[BYTE_LEN+1] = {0}; // 使用sprintf将整数转换为十六进制字符串 // 注意:%llx 是用于无符号 long long 的十六进制格式说明符 // 在大多数平台上,int64_t 和 uint64_t 都是通过 long long 实现的 snprintf(hexStr, sizeof(hexStr), "%llx", uvalue); // 计算实际写入的字符数(不包括终止符'\0') size_t len = strlen(hexStr); // 如果长度小于16,则前面补0 if (len < BYTE_LEN) { std::memmove(hexStr + (BYTE_LEN - len), hexStr, len + 1); // 移动字符串以腾出空间 memset(hexStr, '0', BYTE_LEN - len); // 在前面填充0 } // 将C风格字符串转换为std::string并返回 return std::string(hexStr); } /** * @brife: 将一个 int32 数字转为 十六进制字符串 * @note: int32Value: 0, hexStr:00000000 int32Value: -1, hexStr:ffffffff int32Value: 1, hexStr:00000001 int32Value: 12, hexStr:0000000c int32Value: 123, hexStr:0000007b int32Value: 1234, hexStr:000004d2 int32Value: 12345, hexStr:00003039 int32Value: -12345, hexStr:ffffcfc7 int32Value: 12345678, hexStr:00bc614e */ std::string Int32ToHex(int64_t value) { int BYTE_LEN = 8; // int32_t 占 8 字节,可以表示 8 个 十六进制字符 // 处理负数的一种方式是将其视为无符号数的补码 // 注意:这里我们直接以无符号方式处理,因为十六进制通常不直接表示符号 uint32_t uvalue = static_cast<uint32_t>(value); // 创建一个足够大的字符数组来存储结果(包括终止符'\0') // 32位整数最多需要 8个 十六进制数字加上终止符 char hexStr[BYTE_LEN+1] = {0}; // 使用sprintf将整数转换为十六进制字符串 // 注意:%x 是用于无符号 int 的十六进制格式说明符 snprintf(hexStr, sizeof(hexStr), "%x", uvalue); // 计算实际写入的字符数(不包括终止符'\0') size_t len = strlen(hexStr); // 如果长度小于 8 ,则前面补0 if (len < BYTE_LEN) { std::memmove(hexStr + (BYTE_LEN - len), hexStr, len + 1); // 移动字符串以腾出空间 memset(hexStr, '0', BYTE_LEN - len); // 在前面填充0 } // 将C风格字符串转换为std::string并返回 return std::string(hexStr); } /** * @brife: 将一个 十六进制字符串,转为int64数字 * @note: hexStr:0, int64Value:0 hexStr:1, int64Value:1 hexStr:12, int64Value:18 hexStr:123, int64Value:291 hexStr:1234, int64Value:4660 hexStr:0000000000000001, int64Value:1 hexStr:0000000000000000, int64Value:0 hexStr:0000000000000012, int64Value:18 hexStr:0000000000000123, int64Value:291 hexStr:9030090000106001, int64Value:-8056929837760094207 hexStr:1030090000106001, int64Value:1166442199094681601 */ int64_t HexToInt64(const std::string& hexStr) { int64_t int64Value = 0; size_t BYTE_LEN = 8; // int64_t 是 8 字节(64位) size_t hexStrLen = hexStr.length(); // 如果长度超过了 16 个十六进制字符(即 8 字节),则只取前 16 个字符 if (hexStrLen > 2 * BYTE_LEN) { std::cout << "hexStr is too long, only considering the first " << 2 * BYTE_LEN << " hex characters." << std::endl; hexStrLen = 2 * BYTE_LEN; } // 确保字符串长度是偶数,因为每两个十六进制字符表示一个字节 if (hexStrLen % 2 != 0) { std::cout << "hexStr has an odd number of characters, ignoring the last character." << std::endl; --hexStrLen; } for (size_t i = 0; i < hexStrLen; i += 2) { // 将两个十六进制字符转换为一个字节 char firstChar = hexStr[i]; char secondChar = hexStr[i + 1]; uint8_t byte = 0; // 处理第一个字符(0-9 或 A-F/a-f) if (firstChar >= '0' && firstChar <= '9') { byte = (firstChar - '0') << 4; } else if (firstChar >= 'A' && firstChar <= 'F') { byte = (firstChar - 'A' + 10) << 4; } else if (firstChar >= 'a' && firstChar <= 'f') { byte = (firstChar - 'a' + 10) << 4; } else { // 处理错误,但这里只是简单地忽略无效的字符 std::cout << "Invalid hex character: '" << firstChar << "'" << std::endl; continue; // 跳过当前的两个字符 } // 处理第二个字符 if (secondChar >= '0' && secondChar <= '9') { byte |= (secondChar - '0'); } else if (secondChar >= 'A' && secondChar <= 'F') { byte |= (secondChar - 'A' + 10); } else if (secondChar >= 'a' && secondChar <= 'f') { byte |= (secondChar - 'a' + 10); } else { // 同样地,处理错误或忽略 std::cout << "Invalid hex character: '" << secondChar << "'" << std::endl; continue; // 但在这个例子中,由于我们已经检查了长度是偶数,所以不会执行 } // 将字节合并到 int64_t 中 int64Value = (int64Value << 8) | byte; } return int64Value; } /** * @brife: 将一个 十六进制字符串,转为int32数字 * @note: */ int32_t HexToInt32(const std::string& hexStr) { int32_t int32Value = 0; size_t HEX_CHAR_LEN = 8; // int32_t 是 4 字节,需要 8 个十六进制字符 // 如果长度超过了 8 个十六进制字符(即 4 字节),则只取前 8 个字符 if (hexStr.length() > HEX_CHAR_LEN) { std::cout << "hexStr is too long, only considering the first " << HEX_CHAR_LEN << " hex characters." << std::endl; } // 确保字符串长度是偶数 if (hexStr.length() % 2 != 0) { std::cout << "hexStr has an odd number of characters, ignoring the last character." << std::endl; // 在这里,我们实际上可以选择截断字符串或报错,这里选择截断 HEX_CHAR_LEN--; // 但更好的做法可能是返回一个错误码或抛出异常 } for (size_t i = 0; i < HEX_CHAR_LEN; i += 2) { // 提取两个十六进制字符并转换为一个字节 char firstChar = hexStr[i]; char secondChar = hexStr[i + 1]; uint8_t byte = 0; if ((firstChar >= '0' && firstChar <= '9') || (firstChar >= 'A' && firstChar <= 'F') || (firstChar >= 'a' && firstChar <= 'f')) { byte = (firstChar <= '9') ? (firstChar - '0') : ((firstChar & ~0x20) - 'A' + 10); byte <<= 4; } else { std::cout << "Invalid hex character: '" << firstChar << "'" << std::endl; continue; // 跳过无效字符,但注意这可能会跳过两个字符 } if ((secondChar >= '0' && secondChar <= '9') || (secondChar >= 'A' && secondChar <= 'F') || (secondChar >= 'a' && secondChar <= 'f')) { byte |= (secondChar <= '9') ? (secondChar - '0') : ((secondChar & ~0x20) - 'A' + 10); } else { std::cout << "Invalid hex character: '" << secondChar << "'" << std::endl; // 由于我们已经检查了长度是偶数,这里实际上不会执行 // 但为了完整性,我们仍然可以包含一个错误消息 } // 将字节合并到 int32_t 中 int32Value = (int32Value << 8) | byte; } // 注意:由于我们是从字符串的最低有效字节开始处理的,因此结果将是 big-endian 格式的 // 如果您的系统是小端序的,并且您想要获得与字符串直接对应的值(即小端序),则需要进行字节序转换 return int32Value; } void test_int64_2_hex(){ std::cout<<std::endl<<"=============== test_int64_2_hex =============="<<std::endl; { int64_t int64Value = 0; std::string hexStr = Int64ToHex(int64Value); std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl; } { int64_t int64Value = -1; std::string hexStr = Int64ToHex(int64Value); std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl; } { int64_t int64Value = 1; std::string hexStr = Int64ToHex(int64Value); std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl; } { int64_t int64Value = 1; std::string hexStr = Int64ToHex(int64Value); std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl; } { int64_t int64Value = 12; std::string hexStr = Int64ToHex(int64Value); std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl; } { int64_t int64Value = 123; std::string hexStr = Int64ToHex(int64Value); std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl; } { int64_t int64Value = 1234; std::string hexStr = Int64ToHex(int64Value); std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl; } { int64_t int64Value = 12345; std::string hexStr = Int64ToHex(int64Value); std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl; } { int64_t int64Value = -8056929837760094207; std::string hexStr = Int64ToHex(int64Value); std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl; } { int64_t int64Value = 1166442199094681601; std::string hexStr = Int64ToHex(int64Value); std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl; } { int64_t int64Value = 1166442199094684348; std::string hexStr = Int64ToHex(int64Value); std::cout << "int64Value: " << int64Value<<", hexStr:"<<hexStr << std::endl; } } void test_int32_2_hex(){ std::cout<<std::endl<<"=============== test_int32_2_hex =============="<<std::endl; { int64_t int32Value = 0; std::string hexStr = Int32ToHex(int32Value); std::cout << "int32Value: " << int32Value<<", hexStr:"<<hexStr << std::endl; } { int64_t int32Value = -1; std::string hexStr = Int32ToHex(int32Value); std::cout << "int32Value: " << int32Value<<", hexStr:"<<hexStr << std::endl; } { int64_t int32Value = 1; std::string hexStr = Int32ToHex(int32Value); std::cout << "int32Value: " << int32Value<<", hexStr:"<<hexStr << std::endl; } { int64_t int32Value = 12; std::string hexStr = Int32ToHex(int32Value); std::cout << "int32Value: " << int32Value<<", hexStr:"<<hexStr << std::endl; } { int64_t int32Value = 123; std::string hexStr = Int32ToHex(int32Value); std::cout << "int32Value: " << int32Value<<", hexStr:"<<hexStr << std::endl; } { int64_t int32Value = 1234; std::string hexStr = Int32ToHex(int32Value); std::cout << "int32Value: " << int32Value<<", hexStr:"<<hexStr << std::endl; } { int64_t int32Value = 12345; std::string hexStr = Int32ToHex(int32Value); std::cout << "int32Value: " << int32Value<<", hexStr:"<<hexStr << std::endl; } { int64_t int32Value = -12345; std::string hexStr = Int32ToHex(int32Value); std::cout << "int32Value: " << int32Value<<", hexStr:"<<hexStr << std::endl; } { int64_t int32Value = 12345678; std::string hexStr = Int32ToHex(int32Value); std::cout << "int32Value: " << int32Value<<", hexStr:"<<hexStr << std::endl; } } void test_hex_2_int64(){ std::cout<<std::endl<<"=============== test_hex_2_int64 =============="<<std::endl; { std::string hexStr = "0"; int64_t int64Value = HexToInt64(hexStr); std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl; } { std::string hexStr = "1"; int64_t int64Value = HexToInt64(hexStr); std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl; } { std::string hexStr = "12"; int64_t int64Value = HexToInt64(hexStr); std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl; } { std::string hexStr = "123"; int64_t int64Value = HexToInt64(hexStr); std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl; } { std::string hexStr = "1234"; int64_t int64Value = HexToInt64(hexStr); std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl; } { std::string hexStr = "0000000000000001"; int64_t int64Value = HexToInt64(hexStr); std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl; } { std::string hexStr = "0000000000000000"; int64_t int64Value = HexToInt64(hexStr); std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl; } { std::string hexStr = "0000000000000012"; int64_t int64Value = HexToInt64(hexStr); std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl; } { std::string hexStr = "0000000000000123"; int64_t int64Value = HexToInt64(hexStr); std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl; } { std::string hexStr = "9030090000106001"; int64_t int64Value = HexToInt64(hexStr); std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl; } { std::string hexStr = "1030090000106001"; int64_t int64Value = HexToInt64(hexStr); std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl; } { std::string hexStr = "103009000010600101"; int64_t int64Value = HexToInt64(hexStr); std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl; } { std::string hexStr = "1030090000106AbC"; int64_t int64Value = HexToInt64(hexStr); std::cout<<"hexStr:"<<hexStr<<", int64Value:"<<int64Value<<std::endl; } } void test_hex_2_int32(){ std::cout<<std::endl<<"=============== test_hex_2_int32 =============="<<std::endl; { std::string hexStr = "0"; int64_t int32Value = HexToInt32(hexStr); std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl; } { std::string hexStr = "F"; int64_t int32Value = HexToInt32(hexStr); std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl; } { std::string hexStr = "00"; int64_t int32Value = HexToInt32(hexStr); std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl; } { std::string hexStr = "00000000"; int64_t int32Value = HexToInt32(hexStr); std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl; } { std::string hexStr = "00000001"; int64_t int32Value = HexToInt32(hexStr); std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl; } { std::string hexStr = "00000100"; int64_t int32Value = HexToInt32(hexStr); std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl; } { std::string hexStr = "00010000"; int64_t int32Value = HexToInt32(hexStr); std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl; } { std::string hexStr = "12340000"; int64_t int32Value = HexToInt32(hexStr); std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl; } { std::string hexStr = "00001234"; int64_t int32Value = HexToInt32(hexStr); std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl; } { std::string hexStr = "12345678"; int64_t int32Value = HexToInt32(hexStr); std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl; } { std::string hexStr = "87654321"; int64_t int32Value = HexToInt32(hexStr); std::cout<<"hexStr:"<<hexStr<<", int32Value:"<<int32Value<<std::endl; } } int main() { test_int64_2_hex(); //test_int32_2_hex(); test_hex_2_int64(); //test_hex_2_int32(); return 0; } // g++ convertBCD.cpp -o ./bin/cvt2 -std=c++11