btowc

来自cppreference.com
< c‎ | string‎ | multibyte
在标头 <wchar.h> 定义
wint_t btowc(int c );
(C95 起)

将单字节字符 c(转译成 unsignedchar)加宽为其宽字符等价物。

大多数多字节字符编码都用单字节码来表示来自 ASCII 字符集的字符。此函数可以用于把这些字符转换为 wchar_t

目录

[编辑]参数

c - 要加宽的单字节字符

[编辑]返回值

cEOF 则返回 WEOF

(unsignedchar)c 在初始迁移状态为合法单字节字符,则返回 c 的宽字符表示,否则返回 WEOF

[编辑]示例

#include <stdio.h>#include <wchar.h>#include <locale.h>#include <assert.h>   void try_widen(unsignedchar c){ wint_t w = btowc(c);if(w != WEOF)printf("单字节字符 %#x 加宽为 %#x\n", c, w);elseprintf("单字节字符 %#x 加宽失败\n", c);}   int main(void){char*loc =setlocale(LC_ALL, "lt_LT.iso88594");assert(loc);printf("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("Lithuanian UTF-8 locale 中:\n"); try_widen('A'); try_widen('\xdf'); try_widen('\xf9');}

可能的输出:

Lithuanian ISO-8859-4 locale 中: 单字节字符 0x41 加宽为 0x41 单字节字符 0xdf 加宽为 0xdf 单字节字符 0xf9 加宽为 0x173 Lithuanian UTF-8 locale 中: 单字节字符 0x41 加宽为 0x41 单字节字符 0xdf 加宽失败 单字节字符 0xf9 加宽失败

[编辑]引用

  • C11 标准(ISO/IEC 9899:2011):
  • 7.29.6.1.1 The btowc function (第 441 页)
  • C99 标准(ISO/IEC 9899:1999):
  • 7.24.6.1.1 The btowc function (第 387 页)

[编辑]参阅

(C95)
若可能,收窄宽字符为单字节窄字符
(函数)[编辑]
btowc 的 C++ 文档
close