#include #include #include #include "FormatTrans.h" float HEX2DEC(WORD Hex) { BYTE bai = Hex >> 12; Hex = Hex - bai * std::pow(16, 3); BYTE shi = Hex >> 8; Hex = Hex - shi * pow(16, 2); BYTE ge = Hex >> 4; Hex = Hex - ge * pow(16, 1); BYTE xiao = Hex; return bai * pow(10, 2) + shi * pow(10, 1) + ge * pow(10, 0) + xiao * pow(10, -1); } WORD DEC2HEX(float Dec) { BYTE bai = Dec / pow(10, 2); Dec = Dec - bai * pow(10, 2); BYTE shi = Dec / pow(10, 1); Dec = Dec - shi * pow(10, 1); BYTE ge = Dec / pow(10, 0); Dec = Dec - ge * pow(10, 0); BYTE xiao = Dec / pow(10, -1); return bai * pow(16, 3) + shi * pow(16, 2) + ge * pow(16, 1) + xiao * pow(16, 0); } /*********************************** function: IsHexChar desc: 判断字符是否在0-9,a-f之间 input: char-字符 Output: return: 字符索引,如8-8,a-10,如果 不是,返回-1 ***********************************/ int IsHexChar(char hc) { if('0'<=hc && hc<='9') return (int(hc)-int('0')); else if('a'<=hc && hc<='f') return (int(hc)-int('a')+10); else if('A'<=hc && hc<='F') return (int(hc)-int('A')+10); return -1; } /************************************** function: Hex2Char desc: 16进制数转化为字符 Input: hex-16进制,字符串形式,2字符 Output: return: ascii字符 **************************************/ unsigned char Hex2Char(const std::string &hex) { assert(hex.length() == 2); int high = IsHexChar(hex[0]); int low = IsHexChar(hex[1]); if(high == -1 || low == -1) return '\0'; int asc = high*16+low; // char b = toascii(asc); return asc; } /**************************** function: Hex2String desc: 16进制字符串转化为ascii字符串 Input: hex-16进制字符串 Output: return: ascii字符串 *****************************/ std::string Hex2String(const std::string &hex) { assert(hex.length()%2 == 0); std::string hstr; for(int i=0; i(value); if (intValue < 0) { intValue = 256 + intValue; } std::stringstream ss; ss << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << intValue; return ss.str(); } std::string verify(const std::string& hexStr){ // 准备字符串,去掉空格和0x std::string preparedStr = PrepareHexString(hexStr); // 计算校验码 int sum = 0; for (size_t i = 1; i < 6; i += 1) { int temp = int(static_cast(preparedStr[i])); sum += temp; } // 取模并转换为16进制 int checksum = sum % 0x100; std::string hex = decimalToHexadecimal(checksum); if (hex.length() == 1){ hex = "0" + hex; } return hex; }