Paw*_*shi 2 c++ winapi visual-c++
使用VC++,当我获得本地系统的MAC地址时,它给我一个十六进制代码.我需要将它转换为std :: string,以便我可以在我的代码中使用它.但我无法正常使用它.我知道我必须错过关于c ++中字符串的一个非常基本的概念,请在这里帮助我.
这是我的COED: -
void getMACAddr()
{
CCLOG("getMACAddr 1");
IP_ADAPTER_INFO *info = NULL, *pos;
DWORD size = 0;
GetAdaptersInfo(info, &size);
info = (IP_ADAPTER_INFO *)malloc(size);
GetAdaptersInfo(info, &size);
CCLOG("getMACAddr 2");
for (pos = info; pos != NULL; pos = pos->Next)
{
std::stringstream ss;
CCLOG("getMACAddr 3");
CCLOG("\n%s\n\t", pos->Description);
CCLOG("HEX = :%2.2x", pos->Address[0]);
ss << pos->Address[0];
for (int i = 1; i < pos->AddressLength; i++)
{
CCLOG("HEX = :%2.2x", pos->Address[i]);
ss << pos->Address[i];
}
std::string tS = ss.str();
CCLOG("%s", tS.c_str());
}
free(info);
}
Run Code Online (Sandbox Code Playgroud)
这是我的输出: -
getMACAddr 1
'prog.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dhcpcsvc.dll'.Cannot find or open the PDB file.
getMACAddr 2
getMACAddr 3
Bluetooth Device (Personal Area Network)
HEX = :08
HEX = :3e
HEX = :8e
HEX = :aa
HEX = :37
HEX = :52
tS == >??7R
Run Code Online (Sandbox Code Playgroud)
如你看到的.
最后一行输出("tS ==> ?? 7R")很奇怪.
我该怎么做才能将整个地址放在一个字符串中?
IP_ADAPTER_INFO::Address是一个数组BYTE,所以您可能希望将字节转换为十六进制表示法,而不是将它们填充到字符串中,就好像它们是字符一样.
像这样的东西:
#include <iomanip>
ss << std::hex << std::setfill('0') << std::setw(2);
for (int i = 0; i < pos->AddressLength; i++) {
ss << (int) pos->Address[i];
}
std::string tS = ss.str();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
400 次 |
| 最近记录: |