在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
============================================== 二进制转十六进制 ============================================== //__二进制转十六进制 std::string BinToHex(const std::string &strBin, bool bIsUpper/* = false*/) { std::string strHex; strHex.resize(strBin.size() * 2); for (size_t i = 0; i < strBin.size(); i++) { uint8_t cTemp = strBin[i]; for (size_t j = 0; j < 2; j++) { uint8_t cCur = (cTemp & 0x0f); if (cCur < 10) { cCur += '0'; } else { cCur += ((bIsUpper ? 'A' : 'a') - 10); } strHex[2 * i + 1 - j] = cCur; cTemp >>= 4; } } return strHex; } ============================================== 十六进制转二进制 ============================================== //十六进制转二进制 std::string HexToBin(const std::string &strHex) { if (strHex.size() % 2 != 0) { return ""; } std::string strBin; strBin.resize(strHex.size() / 2); for (size_t i = 0; i < strBin.size(); i++) { uint8_t cTemp = 0; for (size_t j = 0; j < 2; j++) { char cCur = strHex[2 * i + j]; if (cCur >= '0' && cCur <= '9') { cTemp = (cTemp << 4) + (cCur - '0'); } else if (cCur >= 'a' && cCur <= 'f') { cTemp = (cTemp << 4) + (cCur - 'a' + 10); } else if (cCur >= 'A' && cCur <= 'F') { cTemp = (cTemp << 4) + (cCur - 'A' + 10); } else { return ""; } } strBin[i] = cTemp; } return strBin; } 附1:C++二进制字符串转Base64字符串 Base64字符串转二进制字符串 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论