将十六进制值保存到C++字符串

unb*_*ant 3 c++

我有一个简单的问题,但无法在互联网上找到答案.我正在使用Windows的本地WiFi API并尝试获取接入点的MAC.

在WLAN_BSS_ENTRY类型的结构中,有一个名为dot11Bssid的字段,它基本上是一个包含6个无符号字符的数组.

我想要做的是将MAC地址放在std :: string中,如下所示:'AA:AA:AA:AA:AA:AA'.

我可以像这样打印地址:

for (k = 0; k < 6; k++) 
{
    wprintf(L"%02X", wBssEntry->dot11Bssid[k]);
}
Run Code Online (Sandbox Code Playgroud)

但是我无法找到将此值移动到具有上述格式的字符串的方法.

如果你想知道我为什么要在字符串中使用它,我需要帮助,我需要将它与以这种方式格式化的字符串进行比较.在此先感谢您的时间.

hmj*_*mjd 14

使用std::ostringstream(如已经评论过的)IO操纵器.例如:

#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <string>

int main()  
{
    unsigned char buf[] = { 0xAA, 0xD1, 0x09, 0x01, 0x10, 0xF1 };

    std::ostringstream s;
    s << std::hex << std::setfill('0') << std::uppercase
      << std::setw(2) << static_cast<int>(buf[0]) << ':'
      << std::setw(2) << static_cast<int>(buf[1]) << ':'
      << std::setw(2) << static_cast<int>(buf[2]) << ':'
      << std::setw(2) << static_cast<int>(buf[3]) << ':'
      << std::setw(2) << static_cast<int>(buf[4]) << ':'
      << std::setw(2) << static_cast<int>(buf[5]);

    std::cout << "[" << s.str() << "]\n";

    return 0;
}
Run Code Online (Sandbox Code Playgroud)