wctob() function in C++
Last Updated :
14 Sep, 2018
wctob() function in C++ helps to convert a wide character wc into a single byte, if its if its multibyte character equivalent in the initial shift state is a single byte. Since, the function uses single byte encoding, hence is used for characters from ASCII character set .
Syntax:
int wctob (wint_t wc);
Parameters: The above function accepts a single parameter as described below:
wc: This is the only parameter accepted by the wctob() function, it is the wide character needed to be narrowed/converted .
Return Type: The function returns a single byte representation of the wide character wc, if it corresponds to a multibyte character with a length of single byte in initial state. Otherwise, it returns EOF(End Of File).
Below programs illustrate the above function:
Program 1:
#include <bits/stdc++.h>
int fun()
{
int i, num;
const wchar_t wc[] = L "priya lal" ;
num = 0;
for (i = 0; i < wcslen(wc); ++i)
if (wctob(wc[i]) != EOF)
++num;
wprintf(L "wc has %d characters to be translated"
"to single-byte characters." ,
num);
}
int main()
{
fun();
return 0;
}
|
Output:
wc has 9 characters to be translatedto single-byte characters.
Program 2 :
#include <bits/stdc++.h>
void fun( wchar_t wc)
{
int cn = wctob(wc);
if (cn != EOF)
printf ( "%#x translated to %#x\n" , wc, cn);
else
printf ( "%#x could not be translated\n" , wc);
}
int main( void )
{
char * utf_locale_present
= setlocale (LC_ALL, "th_TH.utf8" );
puts ( "In Thai UTF-8 locale:" );
fun(L 'a' );
fun(L '?' );
}
|
Output:
In Thai UTF-8 locale:
0x61 translated to 0x61
0x3f translated to 0x3f
Share your thoughts in the comments
Please Login to comment...