📜  C++ mbrlen()

📅  最后修改于: 2020-09-25 09:34:04             🧑  作者: Mango

C++中的mbrlen() 函数确定多字节字符的大小(以字节为单位)。

mbrlen() 函数在头文件中定义。

mbrlen()原型

size_t mbrlen( const char* s, size_t n, mbstate_t* ps);

mbrlen() 函数检查第一个字节由s指向的字符串 ,并为当前转换状态ps确定其大小(以字节为单位)。检查s最多n个字节。

mbrlen()参数

mbrlen()返回值

mbrlen() 函数返回:

示例:mbrlen() 函数如何工作?

#include 
#include 
#include 
using namespace std;

void test_mbrlen(const char *s, size_t n)
{
    mbstate_t ps = mbstate_t();
    int retVal = mbrlen(s, n, &ps);

    if (retVal == -2)
        cout << "Next " << n << " byte(s) doesn't represent a complete multibyte character" << endl;
    else if (retVal == -1)
        cout << "Next " << n << " byte(s) doesn't represent a valid multibyte character" << endl;
    else
        cout << "Next " << n << " byte(s) of " << s << " holds " << retVal << " byof multibyte character" << endl;
}

int main()
{
    setlocale(LC_ALL, "en_US.utf8");
    char str[] = "\u00b5";

    test_mbrlen(str, 1);
    test_mbrlen(str, 5);

    return 0;
}

运行该程序时,输出为:

Next 1 byte(s) doesn't represent a complete multibyte character
Next 5 byte(s) of µ holds 2 bytes of multibyte character