c++ 2进制转16进制字符串
时间: 2023-08-31 21:08:52 浏览: 154
在C中将二进制转换为十六进制字符串有一个函数可以实现,可以使用下面的方法:
```cpp
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 = cCur;
cTemp >>= 4;
}
}
return strHex;
}
```
这个函数会将输入的二进制字符串转换为十六进制字符串。你可以调用这个函数来实现你的需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [C++二进制字符串转十六进制字符串 十六进制字符串转二进制字符串](https://blog.csdn.net/weixin_30570101/article/details/97098810)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *2* [详解C++中十六进制字符串转数字(数值)](https://download.csdn.net/download/weixin_38614417/14867754)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文