C 標(biāo)準(zhǔn)庫 - <stdlib.h>
C 庫函數(shù) int wctomb(char *str, wchar_t wchar) 把寬字符 wchar 轉(zhuǎn)換為它的多字節(jié)表示形式,并把它存儲在 str 指向的字符數(shù)組的開頭。
下面是 wctomb() 函數(shù)的聲明。
int wctomb(char *str, wchar_t wchar)
下面的示例演示了 wctomb() 函數(shù)的用法。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
wchar_t wc = L'a';
char *pmbnull = NULL;
char *pmb = (char *)malloc(sizeof( char ));
printf("要轉(zhuǎn)換的寬字符:\n");
i = wctomb( pmb, wc );
printf("被轉(zhuǎn)換的字符:%u\n", i);
printf("多字節(jié)字符:%.1s\n", pmb);
printf("當(dāng)要轉(zhuǎn)換的字符為 NULL 時嘗試轉(zhuǎn)換:\n");
i = wctomb( pmbnull, wc );
printf("被轉(zhuǎn)換的字符:%u\n", i);
/* 不會輸出任何值 */
printf("多字節(jié)字符:%.1s\n", pmbnull);
return(0);
}
讓我們編譯并運行上面的程序,這將產(chǎn)生以下結(jié)果:
要轉(zhuǎn)換的寬字符: 被轉(zhuǎn)換的字符:1 多字節(jié)字符:a 當(dāng)要轉(zhuǎn)換的字符為 NULL 時嘗試轉(zhuǎn)換: 被轉(zhuǎn)換的字符:0 多字節(jié)字符: