Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | string‎ | multibyte
m (fmt)
m ({{c}}, headers sorted)
 
Line 5: Line 5:
 
}}
 
}}
  
Widens a single-byte character {{tt|c}} to its wide character equivalent.
+
Widens a single-byte character {{c|c}} to its wide character equivalent.
  
 
Most multibyte character encodings use single-byte codes to represent the characters from the ASCII character set. This function may be used to convert such characters to {{c|wchar_t}}.
 
Most multibyte character encodings use single-byte codes to represent the characters from the ASCII character set. This function may be used to convert such characters to {{c|wchar_t}}.
Line 15: Line 15:
  
 
===Return value===
 
===Return value===
{{lc|WEOF}} if {{tt|c}} is {{lc|EOF}}.
+
{{lc|WEOF}} if {{c|c}} is {{lc|EOF}}.
  
Wide character representation of {{tt|c}} if {{c|(unsigned char)c}} is a valid single-byte character in the initial shift state, {{lc|WEOF}} otherwise.
+
Wide character representation of {{c|c}} if {{c|(unsigned char)c}} is a valid single-byte character in the initial shift state, {{lc|WEOF}} otherwise.
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
 
 
|code=
 
|code=
#include <iostream>
 
#include <cwchar>
 
 
#include <clocale>
 
#include <clocale>
 +
#include <cwchar>
 +
#include <iostream>
  
 
void try_widen(char c)
 
void try_widen(char c)

Latest revision as of 01:06, 9 June 2023

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

Widens a single-byte character c to its wide character equivalent.

Most multibyte character encodings use single-byte codes to represent the characters from the ASCII character set. This function may be used to convert such characters to wchar_t.

Contents

[edit] Parameters

c - single-byte character to widen

[edit] Return value

WEOF if c is EOF.

Wide character representation of c if (unsigned char)c is a valid single-byte character in the initial shift state, WEOF otherwise.

[edit] Example

#include <clocale>
#include <cwchar>
#include <iostream>
 
void try_widen(char c)
{
    std::wint_t w = std::btowc(c);
    if (w != WEOF)
        std::cout << "The single-byte character " << +(unsigned char)c
                  << " widens to " << +w << '\n';
    else
        std::cout << "The single-byte character " << +(unsigned char)c
                  << " failed to widen\n";
}
 
int main()
{
    std::setlocale(LC_ALL, "lt_LT.iso88594");
    std::cout << std::hex << std::showbase << "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
 
    std::setlocale(LC_ALL, "lt_LT.utf8");
    std::cout << "In Lithuanian UTF-8 locale:\n";
    try_widen('A');
    try_widen('\xdf');
    try_widen('\xf9');
}

Possible output:

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

[edit] See also

narrows a wide character to a single-byte narrow character, if possible
(function) [edit]
[virtual]
converts a character or characters from char to CharT
(virtual protected member function of std::ctype<CharT>) [edit]
C documentation for btowc