wctob
來自 cppreference.com
在標頭檔案 <wchar.h> 中定義 |
||
int wctob( wint_t c ); |
(自 C95 起) | |
如果寬字元 c
在初始轉換狀態下對應的多位元組字元是單位元組的,則將其窄化。
這對於 ASCII 字元集中的字元通常是可能的,因為大多數多位元組編碼(例如 UTF-8)都使用單位元組來編碼這些字元。
目錄 |
[編輯] 引數
c | - | 要窄化的寬字元 |
[編輯] 返回值
如果 c
不表示初始轉換狀態下長度為 1 的多位元組字元,則返回 EOF。
否則,返回 c
的單位元組表示,型別為 unsigned char 並轉換為 int。
[編輯] 示例
執行此程式碼
#include <locale.h> #include <wchar.h> #include <stdio.h> #include <assert.h> void try_narrowing(wchar_t c) { int cn = wctob(c); if(cn != EOF) printf("%#x narrowed to %#x\n", c, cn); else printf("%#x could not be narrowed\n", c); } int main(void) { char* utf_locale_present = setlocale(LC_ALL, "th_TH.utf8"); assert(utf_locale_present); puts("In Thai UTF-8 locale:"); try_narrowing(L'a'); try_narrowing(L'๛'); char* tis_locale_present = setlocale(LC_ALL, "th_TH.tis620"); assert(tis_locale_present); puts("In Thai TIS-620 locale:"); try_narrowing(L'a'); try_narrowing(L'๛'); }
可能的輸出
In Thai UTF-8 locale: 0x61 narrowed to 0x61 0xe5b could not be narrowed In Thai TIS-620 locale: 0x61 narrowed to 0x61 0xe5b narrowed to 0xfb