简体   繁体   English

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

[英]Saving hex values to a C++ string

I have a simple question but was not able to find an answer on the internet. 我有一个简单的问题,但无法在互联网上找到答案。 I am using the native WiFi API of Windows and trying to get the MAC of an access point. 我正在使用Windows的本地WiFi API并尝试获取接入点的MAC。

Inside a structure of type WLAN_BSS_ENTRY there is a field named dot11Bssid which is basically an array of 6 unsigned chars. 在WLAN_BSS_ENTRY类型的结构中,有一个名为dot11Bssid的字段,它基本上是一个包含6个无符号字符的数组。

What I want to do, is to have the MAC address in an std::string like this: 'AA:AA:AA:AA:AA:AA'. 我想要做的是将MAC地址放在std :: string中,如下所示:'AA:AA:AA:AA:AA:AA'。

I can print the adress like this: 我可以像这样打印地址:

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

But I am unable to find a way of moving this values to a string with the format identified above. 但我无法找到将此值移动到具有上述格式的字符串的方法。

Help is appreciated, if you wonder why do i want this in a string, I have the need to compare it with a string formatted that way. 如果你想知道为什么我想在字符串中使用它,我需要帮助,我需要将它与以这种方式格式化的字符串进行比较。 Thanks in advance for your time. 在此先感谢您的时间。

Use std::ostringstream (as already commented) with the IO manipulators . 使用带有IO操纵器的 std::ostringstream (已经注释)。 For example : 例如

#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;
}

I would assume you'd use something similar to sprintf, the std::string class contains a conversion constructor to move from c-strings to std::strings. 我假设您使用类似于sprintf的东西,std :: string类包含一个转换构造函数,用于从c-strings移动到std :: strings。 So you could sprintf(), if that's what you're looking for. 所以你可以sprintf(),如果这是你正在寻找的。 You're using wchar types though, so you may want to consider swprintf() instead. 您正在使用wchar类型,因此您可能需要考虑使用swprintf()。

Similar question: std::string formatting like sprintf 类似的问题: std :: string格式化如sprintf

There's nothing wrong with using stringstream, but it's sort of surprising that one would know wprintf without being aware of swprintf. 使用stringstream没有任何问题,但是如果不知道swprintf就会知道wprintf,这有点令人惊讶。 printf is a whole family of functions, not just printf and wprintf: printf是一整套函数,而不仅仅是printf和wprintf:

​int printf( const char *format, ... );​
int fprintf( FILE *stream, const char *format, ... );
int sprintf( char *buffer, const char *format, ... );
int snprintf( char *buffer, int buf_size, const char *format, ... );
int wprintf( const wchar_t* format, ... );
int fwprintf( FILE *stream, const wchar_t* format, ... );
int swprintf( char *buffer, const wchar_t* format, ... );
​int vprintf( const char *format, va_list vlist );​
int vfprintf( FILE *stream, const char *format, va_list vlist );
int vsprintf( const char *buffer, const char *format, va_list vlist );
int vsnprintf( char *buffer, int buf_size, const char *format, va_list vlist );
int vwprintf( const wchar_t* format, va_list vlist );
int vfwprintf( FILE* stream, const wchar_t* format, va_list vlist );(2) 
int vswprintf( const wchar_t* buffer, const wchar_t* format, va_list vlist );

Use some like that 使用类似的东西

int value = 0x1234;
char * temp = new char[100];
sprintf(temp, "0x%x", value);
std::string target(temp);
delete[] temp;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM