名稱空間
變體
操作

wctob

來自 cppreference.com
< c‎ | string‎ | multibyte
在標頭檔案 <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

[編輯] 參考

  • C11 標準 (ISO/IEC 9899:2011)
  • 7.29.6.1.2 wctob 函式 (p: 441)
  • C99 標準 (ISO/IEC 9899:1999)
  • 7.24.6.1.2 wctob 函式 (p: 387)

[編輯] 另請參見

(C95)
如果可能,將單位元組窄字元加寬為寬字元
(函式) [編輯]
C++ 文件 中的 wctob