C 教程
C 標(biāo)準(zhǔn)庫 - <stdlib.h>
C 庫函數(shù) int wctomb(char *str, wchar_t wchar) 把寬字符 wchar 轉(zhuǎn)換為它的多字節(jié)表示形式,并把它存儲(chǔ)在 str 指向的字符數(shù)組的開頭。
下面是 wctomb() 函數(shù)的聲明。
int wctomb(char *str, wchar_t wchar)
下面的實(shí)例演示了 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)換的字符:%un", i); printf("多字節(jié)字符:%.1sn", pmb); printf("當(dāng)要轉(zhuǎn)換的字符為 NULL 時(shí)嘗試轉(zhuǎn)換:n"); i = wctomb( pmbnull, wc ); printf("被轉(zhuǎn)換的字符:%un", i); /* 不會(huì)輸出任何值 */ printf("多字節(jié)字符:%.1sn", pmbnull); return(0); }
讓我們編譯并運(yùn)行上面的程序,這將產(chǎn)生以下結(jié)果:
要轉(zhuǎn)換的寬字符: 被轉(zhuǎn)換的字符:1 多字節(jié)字符:a 當(dāng)要轉(zhuǎn)換的字符為 NULL 時(shí)嘗試轉(zhuǎn)換: 被轉(zhuǎn)換的字符:0 多字節(jié)字符:其他擴(kuò)展