C++: Type Conversions

typedef std::wstring XSTRING;

void Convert(LPCTSTR input, XSTRING &output)
{
output = input;
}

void Convert(int input, XSTRING &output)
{
output = L"";

std::wostringstream ws;
ws << input;
output = XSTRING(ws.str());
}

void Convert(unsigned int input, XSTRING &output)
{
output = L"";

std::wostringstream ws;
ws << input;
output = XSTRING(ws.str());
}

void Convert(unsigned long input, XSTRING &output)
{
output = L"";

std::wostringstream ws;
ws << input;
output = XSTRING(ws.str());
}

void Convert(std::string input, XSTRING &output)
{
output = L"";

output = XSTRING(input.begin(), input.end());
}

void Convert(XSTRING input, std::string &output)
{
output = "";

output = std::string(input.begin(), input.end());
}

void Convert(char* input, XSTRING &output)
{
output = L"";

output = XSTRING(input, input + strlen(input));
}

void Convert(XSTRING input, long &output)
{
  output = 0;
  std::wistringstream wstrm(input);
  wstrm >> output;
}

void Convert(XSTRING input, unsigned short &output)
{
  char tmp[32];
  for(int index=0; index<32 br="" index="">     tmp[index]='\0';
  }

  Convert(input.c_str(),tmp, sizeof(tmp));
  output = (unsigned short)atoi(tmp);
}

void Convert(const wchar_t *input, char* output, int size)
{
  wcstombs ( output, input, size);
}

void Convert(wchar_t *input, XSTRING &output)
{
  output = XSTRING(input);
}

Comments

  1. These compilation of type conversion is very useful. It helped me in my projects very much!

    ReplyDelete

Post a Comment

Popular posts from this blog

Creating tray application and popup context menu in Windows

How to statically link applications to wxWidgets via Visual Studio 2008 and in Linux

How to put an image to wxPanel using wxWidgets