Open In App

wctob() function in C++

Improve
Improve
Like Article
Like
Save
Share
Report

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:




    // 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;
    }

    
    

    Output:

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

    Program 2 :




    // 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'?');
    }

    
    

    Output:

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


    Last Updated : 14 Sep, 2018
    Like Article
    Save Article
    Previous
    Next
    Share your thoughts in the comments
    Similar Reads