名稱空間
變體
操作

btowc

來自 cppreference.com
< c‎ | string‎ | multibyte
在標頭檔案 <wchar.h> 中定義
wint_t btowc( int c );
(自 C95 起)

將單位元組字元 c(重新解釋為 unsigned char)擴充套件為對應的寬字元。

大多數多位元組字元編碼使用單位元組程式碼來表示 ASCII 字元集中的字元。此函式可用於將此類字元轉換為 wchar_t

目錄

[編輯] 引數

c - 要擴充套件的單位元組字元

[編輯] 返回值

如果 cEOF,則為 WEOF

如果 (unsigned char)c 在初始移位狀態下是有效的單位元組字元,則返回 c 的寬字元表示;否則返回 WEOF

[編輯] 示例

#include <stdio.h>
#include <wchar.h>
#include <locale.h>
#include <assert.h>
 
void try_widen(unsigned char c)
{
    wint_t w = btowc(c);
    if(w != WEOF)
        printf("The single-byte character %#x widens to %#x\n", c, w);
    else
        printf("The single-byte character %#x failed to widen\n", c);
}
 
int main(void)
{
    char *loc = setlocale(LC_ALL, "lt_LT.iso88594");
    assert(loc);
    printf("In Lithuanian ISO-8859-4 locale:\n");
    try_widen('A');
    try_widen('\xdf'); // German letter ß (U+00df) in ISO-8859-4
    try_widen('\xf9'); // Lithuanian letter ų (U+0173) in ISO-8859-4
 
    setlocale(LC_ALL, "lt_LT.utf8");
    printf("In Lithuanian UTF-8 locale:\n");
    try_widen('A');
    try_widen('\xdf');
    try_widen('\xf9');
}

可能的輸出

In Lithuanian ISO-8859-4 locale:
The single-byte character 0x41 widens to 0x41
The single-byte character 0xdf widens to 0xdf
The single-byte character 0xf9 widens to 0x173
In Lithuanian UTF-8 locale:
The single-byte character 0x41 widens to 0x41
The single-byte character 0xdf failed to widen
The single-byte character 0xf9 failed to widen

[編輯] 參考

  • C11 標準 (ISO/IEC 9899:2011)
  • 7.29.6.1.1 btowc 函式 (p: 441)
  • C99 標準 (ISO/IEC 9899:1999)
  • 7.24.6.1.1 btowc 函式 (p: 387)

[編輯] 參閱

(C95)
如果可能,將寬字元縮小為單位元組窄字元
(function) [編輯]
btowc 的 C++ 文件