名前空間
変種
操作

btowc

From cppreference.com
< c‎ | string‎ | multibyte
ヘッダー <wchar.h> で定義
wint_t btowc( int c );
(C95 以降)

単バイト文字cunsigned charとして再解釈される)を、そのワイド文字相当に変換します。

ほとんどのマルチバイト文字エンコーディングは、ASCII文字セットの文字を表すために単バイトコードを使用します。この関数は、そのような文字をwchar_tに変換するために使用できます。

目次

[編集] パラメータ

c - 変換する単バイト文字

[編集] 戻り値

cEOFの場合、WEOF

cのワイド文字表現。ただし、(unsigned char)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 The btowc function (p: 441)
  • C99標準 (ISO/IEC 9899:1999)
  • 7.24.6.1.1 The btowc function (p: 387)

[編集] 関連項目

(C95)
可能であれば、ワイド文字を1バイトのナロー文字にナロー化する
(関数) [編集]
English 日本語 中文(简体) 中文(繁體)