#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 = 0x00; int BYTE_LEN = 16; //如果长度超过了 16 字节,则只取前16字节 if(hexStr.length() > BYTE_LEN){ std::cout<<"hexStr is out of int64 range, the back data will lost! hexStr.len:"<<hexStr.length()<<std::endl; } for(int i=0; i<hexStr.length() && i<BYTE_LEN; ++i){ int8_t oneByte = hexStr.at(i) - '0'; int64Value = int64Value<<4 | oneByte; } return int64Value; } /** * @brife: 将一个 十六进制字符串,转为int32数字 * @note: */ int32_t HexToInt32(const std::string& hexStr) { int32_t int32Value = 0x00; int BYTE_LEN = 8; //如果长度超过了 8 字节,则只取前8字节 if(hexStr.length() > BYTE_LEN){ std::cout<<"hexStr is out of int32 range, the back data will lost! hexStr.len:"<<hexStr.length()<<std::endl; } for(int i=0; i<hexStr.length() && i<BYTE_LEN; ++i){ int8_t oneByte = hexStr.at(i) - '0'; int32Value = int32Value<<4 | oneByte; } 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; } } 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; } } 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
标签:std,十六进制,int32,C++,LEN,int64Value,BYTE,hexStr From: https://www.cnblogs.com/music-liang/p/18352114