Created
June 16, 2015 03:25
-
-
Save xebecnan/6d070c93fb69f40c3673 to your computer and use it in GitHub Desktop.
win32: convert between wchar and utf8
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
wchar_t* | |
fromUTF8( | |
const char* src, | |
size_t src_length, /* = 0 */ | |
size_t* out_length /* = NULL */ | |
) | |
{ | |
if(!src) | |
{ return NULL; } | |
if(src_length == 0) { src_length = strlen(src); } | |
int length = MultiByteToWideChar(CP_UTF8, 0, src, src_length, 0, 0); | |
wchar_t *output_buffer = (wchar_t*)malloc((length+1) * sizeof(wchar_t)); | |
if(output_buffer) { | |
MultiByteToWideChar(CP_UTF8, 0, src, src_length, output_buffer, length); | |
output_buffer[length] = L'\0'; | |
} | |
if(out_length) { *out_length = length; } | |
return output_buffer; | |
} | |
char* | |
toUTF8( | |
const wchar_t* src, | |
size_t src_length, /* = 0 */ | |
size_t* out_length /* = NULL */ | |
) | |
{ | |
if(!src) | |
{ return NULL; } | |
if(src_length == 0) { src_length = wcslen(src); } | |
int length = WideCharToMultiByte(CP_UTF8, 0, src, src_length, | |
0, 0, NULL, NULL); | |
char *output_buffer = (char*)malloc((length+1) * sizeof(char)); | |
if(output_buffer) { | |
WideCharToMultiByte(CP_UTF8, 0, src, src_length, | |
output_buffer, length, NULL, NULL); | |
output_buffer[length] = '\0'; | |
} | |
if(out_length) { *out_length = length; } | |
return output_buffer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment