Skip to content

Instantly share code, notes, and snippets.

@Akira-Hayasaka
Last active October 10, 2015 12:28
Show Gist options
  • Save Akira-Hayasaka/3689867 to your computer and use it in GitHub Desktop.
Save Akira-Hayasaka/3689867 to your computer and use it in GitHub Desktop.
c++ converting string to wstring
static int strConv(const string &src, wstring &dst)
{
iconv_t cd = iconv_open("UCS-4-INTERNAL", "UTF-8");
if (cd == (iconv_t)-1)
return -1;
size_t src_length = strlen(src.c_str());
int wlen = (int)src_length/3;
size_t buf_length = src_length + wlen;
char src_buf[src_length];
strcpy(src_buf, src.c_str());
char *buf = new char [buf_length];
char *inptr = src_buf;
char *outptr = buf;
if (iconv(cd, &inptr, &src_length, &outptr, &buf_length) == -1)
{
if (buf!=NULL)
delete [] buf;
return -1;
}
iconv_close(cd);
dst = wstring(reinterpret_cast<wchar_t*>(buf));
dst = dst.substr(0, wlen);
if (buf!=NULL)
delete [] buf;
return wlen;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment