Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/multibyte/wctob"

From cppreference.com
< cpp‎ | string‎ | multibyte
(+see c)
m ({{c}}, ., fmt)
 
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{cpp/title|wctob}}
 
{{cpp/title|wctob}}
{{cpp/string/multibyte/sidebar}}
+
{{cpp/string/multibyte/navbar}}
{{ddcl | header=cwchar |
+
{{ddcl|header=cwchar|
 
int wctob( std::wint_t c );
 
int wctob( std::wint_t c );
 
}}
 
}}
  
Narrows a wide character {{tt|c}} if its multibyte character equivalent in the initial shift state is a single byte.
+
Narrows a wide character {{c|c}} if its multibyte character equivalent in the initial shift state is a single byte.
  
 
This is typically possible for the characters from the ASCII character set, since most multibyte encodings (such as UTF-8) use single bytes to encode those characters.
 
This is typically possible for the characters from the ASCII character set, since most multibyte encodings (such as UTF-8) use single bytes to encode those characters.
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | c | wide character to narrow}}
+
{{par|c|wide character to narrow}}
{{param list end}}
+
{{par end}}
  
 
===Return value===
 
===Return value===
{{c|EOF}} if {{tt|c}} does not represent a multibyte character with length {{c|1}} in initial shift state.
+
{{lc|EOF}} if {{c|c}} does not represent a multibyte character with length {{c|1}} in initial shift state.
  
Otherwise, the single-byte representation of {{tt|c}} as {{c|unsigned char}} converted to {{c|int}}
+
Otherwise, the single-byte representation of {{c|c}} as {{c|unsigned char}} converted to {{c|int}}.
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
#include <cwchar>
+
#include <cstdio>
+
 
#include <clocale>
 
#include <clocale>
 +
#include <cwchar>
 +
#include <iostream>
  
 +
void try_narrowing(wchar_t c)
 +
{
 +
    int cn = std::wctob(c);
 +
    if (cn != EOF)
 +
        std::cout << '\'' << int(c) << "' narrowed to " << +cn << '\n';
 +
    else
 +
        std::cout << '\'' << int(c) << "' could not be narrowed\n";
 +
}
 +
 
int main()
 
int main()
 
{
 
{
     std::setlocale(LC_ALL, "");
+
     std::setlocale(LC_ALL, "th_TH.utf8");
     std::printf("narrow: %c\nnarrowed from wide: %c\n", 'a', std::wctob(L'a'));
+
     std::cout << std::hex << std::showbase << "In Thai UTF-8 locale:\n";
 +
    try_narrowing(L'a');
 +
    try_narrowing(L'๛');
 +
 
 +
    std::setlocale(LC_ALL, "th_TH.tis620");
 +
    std::cout << "In Thai TIS-620 locale:\n";
 +
    try_narrowing(L'a');
 +
    try_narrowing(L'๛');
 
}
 
}
| output=
+
|output=
narrow: a
+
In Thai UTF-8 locale:
narrowed from wide: a
+
'0x61' narrowed to 0x61
 +
'0xe5b' could not be narrowed
 +
In Thai TIS-620 locale:
 +
'0x61' narrowed to 0x61
 +
'0xe5b' narrowed to 0xfb
 
}}
 
}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/string/multibyte/dcl list btowc}}
+
{{dsc inc|cpp/string/multibyte/dsc btowc}}
{{dcl list template | cpp/locale/ctype/dcl list do_narrow}}
+
{{dsc inc|cpp/io/basic_ios/dsc narrow}}
{{dcl list see c | c/string/multibyte/wctob}}
+
{{dsc inc|cpp/locale/ctype/dsc narrow}}
{{dcl list end}}
+
{{dsc see c|c/string/multibyte/wctob}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 22:51, 9 June 2023

Defined in header <cwchar>
int wctob( std::wint_t c );

Narrows a wide character c if its multibyte character equivalent in the initial shift state is a single byte.

This is typically possible for the characters from the ASCII character set, since most multibyte encodings (such as UTF-8) use single bytes to encode those characters.

Contents

[edit] Parameters

c - wide character to narrow

[edit] Return value

EOF if c does not represent a multibyte character with length 1 in initial shift state.

Otherwise, the single-byte representation of c as unsigned char converted to int.

[edit] Example

#include <clocale>
#include <cwchar>
#include <iostream>
 
void try_narrowing(wchar_t c)
{
    int cn = std::wctob(c);
    if (cn != EOF)
        std::cout << '\'' << int(c) << "' narrowed to " << +cn << '\n';
    else
        std::cout << '\'' << int(c) << "' could not be narrowed\n";
}
 
int main()
{
    std::setlocale(LC_ALL, "th_TH.utf8");
    std::cout << std::hex << std::showbase << "In Thai UTF-8 locale:\n";
    try_narrowing(L'a');
    try_narrowing(L'๛');
 
    std::setlocale(LC_ALL, "th_TH.tis620");
    std::cout << "In Thai TIS-620 locale:\n";
    try_narrowing(L'a');
    try_narrowing(L'๛');
}

Output:

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

[edit] See also

widens a single-byte narrow character to wide character, if possible
(function) [edit]
narrows characters
(public member function of std::basic_ios<CharT,Traits>) [edit]
invokes do_narrow
(public member function of std::ctype<CharT>) [edit]
C documentation for wctob