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'); // ISO-8859-4的德文字母ß(U+00df)
    try_widen('\xf9'); // ISO-8859-4的立陶宛文字母ų(U+0173)
 
    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 standard (ISO/IEC 9899:2011):
  • 7.29.6.1.1 The btowc function (p: 441)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.24.6.1.1 The btowc function (p: 387)

[编辑] 参阅

(C95)
将宽字符收窄成单字节窄字符,倘若可能
(函数) [编辑]
btowcC++ 文档