wcstombs示例:
/* wcstombs example */
#include <stdio.h> /* printf */
#include <stdlib.h> /* wcstombs, wchar_t(C) */
int main() {
const wchar_t str[] = L"wcstombs example";
char buffer[32];
int ret;
printf ("wchar_t string: %ls \n",str);
ret = wcstombs ( buffer, str, sizeof(buffer) );
if (ret==32) buffer[31]='\0';
if (ret) printf ("multibyte string: %s \n",buffer);
return 0;
}
输出:
- wchar_t string: wcstombs example
- multibyte string: wcstombs example
//mbstowcs == multibyte string to wide character string
int requiredSize = mbstowcs(NULL, string, 0); //如果mbstowcs第一参数为NULL那么返回字符串的数目 requiredSize为string转为宽字符后的宽字符字符数,注意这个数目不包含结尾符
wchar_t* wText = new wchar_t[requiredSize + 1]; //+1以保留一个位置放置结尾符 0/
mbstowcs(wText, string, requiredSize + 1);//由char转换成wchar类型