mbrlen() function in C/C++

The mbrlen() function in C/C++ determines the size in bytes, of the remainder of the multibyte character whose first byte is pointed to by str, given the current conversion state ps. The behavior of this function depends on the LC_CTYPE category of the selected C locale.

Syntax:

size_t mbrlen( const char* str, size_t n, mbstate_t* ps)

Parameters: The function accepts three mandatory parameters which are described below:

  • str: specifies the pointer to the first byte of multibyte string to examine
  • n: specifies the mAximum number of bytes in s to examine
  • ps: specifies the pointer to mbstate_t object that defines a conversion state

Return value : The function returns four value as below:

  1. the number of bytes which complete a valid multibyte character
  2. -1 if encoding error occurs
  3. 0 if s points to null character
  4. -2 if the next n bytes are part of a possibly valid multibyte character, which is still incomplete after examining all n bytes

Below programs illustrate the above function:
Program 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// mbrlen() function
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the size of
// the multibyte character
void check_(const char* str, size_t n)
{
    // Multibyte conversion state
    mbstate_t ps = mbstate_t();
  
    // number of byte to be saved in returnV
    int returnV = mbrlen(str, n, &ps);
  
    if (returnV == -2)
        cout << "Next " << n << " byte(s) doesn't"
             << " represent a complete"
             << " multibyte character" << endl;
  
    else if (returnV == -1)
        cout << "Next " << n << " byte(s) doesn't "
             << "represent a valid multibyte character" << endl;
    else
        cout << "Next " << n << " byte(s) of "
             << str << "holds " << returnV << " byte"
             << " multibyte character" << endl;
}
  
// Driver code
int main()
{
    setlocale(LC_ALL, "en_US.utf8");
    char str[] = "\u10000b5";
  
    // test for first 1 byte
    check_(str, 1);
  
    // test for first 6 byte
    check_(str, 6);
  
    return 0;
}

chevron_right


Output:

Next 1 byte(s) doesn't represent a complete multibyte character
Next 6 byte(s) of á??0b5holds 3 byte multibyte character

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// mbrlen() function
// with empty string
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the size of the multibyte character
void check_(const char* str, size_t n)
{
    // Multibyte conversion state
    mbstate_t ps = mbstate_t();
  
    // number of byte to be saved in returnV
    int returnV = mbrlen(str, n, &ps);
  
    if (returnV == -2)
        cout << "Next " << n << " byte(s) doesn't"
             << " represent a complete"
             << " multibyte character" << endl;
  
    else if (returnV == -1)
        cout << "Next " << n << " byte(s) doesn't "
             << "represent a valid multibyte character" << endl;
    else
        cout << "Next " << n << " byte(s) of "
             << str << "holds " << returnV << " byte"
             << " multibyte character" << endl;
}
  
// Driver code
int main()
{
    setlocale(LC_ALL, "en_US.utf8");
    char str[] = "";
  
    // test for first 1 byte
    check_(str, 1);
  
    // test for first 3 byte
    check_(str, 3);
  
    return 0;
}

chevron_right


Output:

Next 1 byte(s) of holds 0 byte multibyte character
Next 3 byte(s) of holds 0 byte multibyte character


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.