Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

char* to const wchar_t * conversion

I need to convert character pointer to a w_char * in order to use ParseNetworkString(). I've tried finding solutions to this myself, and while I have found one solution, there is one issue which blocks me from using it:

b1naryatr0phy said in an other post:

std::wstring name( L"Steve Nash" );
const wchar_t* szName = name.c_str();

this almost works for me, except that I can't just pass the string explicity, as its value is not always going to be the same, meaning I can't just put it in quotes. If I replace the parameter with a function call, then the first line gives me an error (example: std::wstring name(LgetIpAddress()); I've tried std::wstring name(L" " + getIpAddress() + " "); , thinking that it just needs a quotation mark after the L, but that doesn't work either.. any suggestions?

Thanks! :)

Extra Kudos to whoever can answer this!

link to other post: I want to convert std::string into a const wchar_t *

P.S. Sorry, just to clarify, getIpAddress returns a character pointer

like image 681
user1553248 Avatar asked Aug 07 '12 20:08

user1553248


1 Answers

What's the type of getIpAddress()?

Here's a snippet showing how to convert a string to a wstring (using standard C++11, no error checking):

wstring to_wstring(string const& str)
{
  size_t len = mbstowcs(nullptr, &str[0], 0);
  if (len == -1) // invalid source str, throw
  wstring wstr(len, 0);
  mbstowcs(&wstr[0], &str[0], wstr.size());
  return wstr;
}
like image 178
eq- Avatar answered Sep 20 '22 10:09

eq-