3

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.

Inside a structure of type WLAN_BSS_ENTRY there is a field named dot11Bssid which is basically an array of 6 unsigned chars.

What I want to do, is to have the MAC address in an std::string like this: '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.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
unbekant
  • 1,555
  • 22
  • 31

4 Answers4

14

Use std::ostringstream (as already commented) with the IO manipulators. 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;
}
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • It worked well with std::ostringstream and for anyone visiting the question this answer will be really useful, so marked as correct answer! Thanks. – unbekant Jun 24 '12 at 21:48
  • 1
    This is "The Right Way to Do It" for C++, so upvote. Could be a bit improved with some sort of foreach or range-based for construction as far as verbosity is concerned. – Robert Mason Jun 24 '12 at 22:20
1

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. So you could sprintf(), if that's what you're looking for. You're using wchar types though, so you may want to consider swprintf() instead.

Similar question: std::string formatting like sprintf

Community
  • 1
  • 1
TReed0803
  • 585
  • 3
  • 9
  • I'll give it a try and tell you how did I do. Thanks! – unbekant Jun 24 '12 at 21:32
  • When I commented on this, I forgot about `std::stringstream`, it may very well be a better idea to go that route. Regardless, I hope you can get it working! :) – TReed0803 Jun 24 '12 at 21:36
  • It worked fine both ways but I'll keep the one with std::stringstream which seems better. For the sake of the site I'll mark the other one as correct but thanks anyway :) . – unbekant Jun 24 '12 at 21:47
1

There's nothing wrong with using stringstream, but it's sort of surprising that one would know wprintf without being aware of swprintf. printf is a whole family of functions, not just printf and 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 );
bames53
  • 86,085
  • 15
  • 179
  • 244
  • It was my bad, it was a long time since I last dealt with C/C++ and was a bit rusty but I was aware of the printf family. – unbekant Jun 24 '12 at 22:14
0

Use some like that

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