Namespaces
Variants

wctob

From cppreference.net
定義先ヘッダ <wchar.h>
int wctob ( wint_t c ) ;
(C95以降)

ワイド文字 c のマルチバイト文字相当が初期シフト状態で単一バイトの場合、その文字をナロー化します。

これは通常、ASCII文字セットの文字に対して可能です。なぜなら、ほとんどのマルチバイトエンコーディング(UTF-8など)はそれらの文字をエンコードするためにシングルバイトを使用するためです。

目次

パラメータ

c - ワイド文字からナロー文字へ

戻り値

EOF もし c が初期シフト状態で長さ 1 のマルチバイト文字を表さない場合。

それ以外の場合、 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)
シングルバイトナロー文字をワイド文字に変換(可能な場合)
(関数)