-1

I just want to convert my wchar array to a string and push it in my string vector. My solutions are commented out because it doesn't work. I get an error that my vector is overloaded. Code is below:

  vector<wstring> vec;
  string tmp;
  int n;
  int m_device_id = 0;
  do {
    m_device_id++;
    tmp="";
    wprintf(L"\tDevice %d:\r\n", m_device_id);
    wprintf(L"\t\tName: %s\r\n", m_device_info.szName);
    wprintf(L"\t\tAddress: %02x:%02x:%02x:%02x:%02x:%02x\r\n", m_device_info.Address.rgBytes[0], m_device_info.Address.rgBytes[1], m_device_info.Address.rgBytes[2], m_device_info.Address.rgBytes[3], m_device_info.Address.rgBytes[4], m_device_info.Address.rgBytes[5]);
    wprintf(L"\t\tClass: 0x%08x\r\n", m_device_info.ulClassofDevice);

wostringstream tmp;
    for (int i = 0; i < 6; i++)
    {
     tmp << m_device_info.Address.rgBytes [i];

    // Append the colon, but not after the last
    if (i < 5)
    tmp << L':';
   }
    vec.push_back(tmp.str());

  } while(BluetoothFindNextDevice(m_bt_dev, &m_device_info));


  vector<wstring>::iterator it = find(vec.begin(), vec.end(), wstring(L"18:22:85:d8:03:98"));
  if(it != vec.end()) {
  cout << "Found it" << '\n';
  } else {
  cout << "Not found" << '\n';
 }
  BluetoothFindDeviceClose(m_bt_
 }while(BluetoothFindNextRadio(&m_bt_find_radio, &m_radio));

===============================================

Structure of Device Info

typedef struct _BLUETOOTH_DEVICE_INFO {
 DWORD             dwSize;
 BLUETOOTH_ADDRESS Address;
 ULONG             ulClassofDevice;
 BOOL              fConnected;
 BOOL              fRemembered;
 BOOL              fAuthenticated;
 SYSTEMTIME        stLastSeen;
 SYSTEMTIME        stLastUsed;
 WCHAR             szName[BLUETOOTH_MAX_NAME_SIZE];
} BLUETOOTH_DEVICE_INFO;
Kipcak08
  • 313
  • 2
  • 4
  • 13

1 Answers1

2

First of all, if you ask a question about compilation errors, always put the errors in the question.

Secondly, the errors you are getting is because you have a vector containing wstring, but try to push a variable of type string.

Thirdly, judging by your code, the array you want to create a string from is not an array of characters, but an array of numbers. You can create a string of those be using e.g. std::wostringstream:

std::wostringstream tmp;
for (int i = 0; i < 6; i++)
{
    tmp << std::hex << std::setw(2) << std::setfill(L'0')
        << m_device_info.Address.rgBytes[i];

    // Append the colon, but not after the last
    if (i < 5)
        tmp << L':';
}

vec.push_back(tmp.str());
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • If you get errors, do _not_ edit the answer to say that. Instead add it as a comment. Regarding the errors you get, are you including ``? – Some programmer dude Jul 27 '12 at 12:58
  • Im sorry for this! Ok, now I have only one new error. The error is on tmp<< m_device_info.Adress**[**i] It says that the the typ is not ok for the datastructure in Bluetoot_adress_struct. – Kipcak08 Jul 27 '12 at 13:09
  • I have only this one error! Please help me sombody here :) – Kipcak08 Jul 27 '12 at 13:22
  • @user1557170 Ah, I should have used `rgBytes` as the array. Edited answer to correct that. – Some programmer dude Jul 27 '12 at 13:23
  • first of all thanks a lot! I have only the problem is not the right value in my tmp. Because Im searching for a specified wstring. And If I look in Debug mode I see in my vec an other value "24:34:133:216:3:152"... I have edited my code with the search algorithm. Look below – Kipcak08 Jul 27 '12 at 13:32
  • @user1557170 You want hexadecimal numbers, edited answer again, don't forget to include ``. I would really recommend you to read more about basic C++, specially stream handling and formatting. – Some programmer dude Jul 27 '12 at 13:40
  • thanks for the nice support. I need this stuff only for demonstration and testing if its possible what I think in C++. If its like this, Ill read more about basic c++. I have one last question. In the converting process its deleting the 0. So the result of my value is 18:22:85:d8:3:98 <- instead of 18:22:85:d8:03:98 ? Can I catch the zero also in my formatting thing ? Thanks a lot. – Kipcak08 Jul 27 '12 at 13:49
  • @user1557170 Edited my answer once more, to add the formatting. – Some programmer dude Jul 27 '12 at 13:53
  • ok, last time than I'll will not get on your balls :-) After setw(2)**<<**setfill I get an error that the operator is not compatible. – Kipcak08 Jul 27 '12 at 13:57
  • Also whats the difference between this two operators ? Its seems like that something its missing... – Kipcak08 Jul 27 '12 at 14:06
  • @user1557170 I wasn't using a wide character for `setfill`. Answer updated – Some programmer dude Jul 27 '12 at 14:09
  • I see now in the debug mode that the value of my tmp is fully incorrect. It has now filled 64 Array with a lot of stuff. I think the filling of the zeros is not working correctly :-( – Kipcak08 Jul 27 '12 at 14:21