Last active
October 10, 2015 12:28
-
-
Save Akira-Hayasaka/3689867 to your computer and use it in GitHub Desktop.
c++ converting string to wstring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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