wctob() function in C++

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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Program illustrating
    // wctob() function
    #include <bits/stdc++.h>
      
    // function implementing the wctob() function
    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;
      
        // prints the numbers of characters
        // needed to be translated
        wprintf(L"wc has %d characters to be translated"
                "to single-byte characters.",
                num);
    }
      
    // Driver Program
    int main()
    {
        fun();
        return 0;
    }

    chevron_right

    
    

    Output:

    wc has 9 characters to be translatedto single-byte characters.
    

    Program 2 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program illustrating the wctob() function
    #include <bits/stdc++.h>
      
    // function implementing the wctob() function
    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);
    }
      
    // Driver Program
    int main(void)
    {
        char* utf_locale_present
            = setlocale(LC_ALL, "th_TH.utf8");
      
        // prints the result of the function
        puts("In Thai UTF-8 locale:");
        fun(L'a');
        fun(L'?');
    }

    chevron_right

    
    

    Output:

    In Thai UTF-8 locale:
    0x61 translated to 0x61
    0x3f translated to 0x3f
    


    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.