📅  最后修改于: 2020-09-25 09:34:04             🧑  作者: Mango
mbrlen() 函数在
size_t mbrlen( const char* s, size_t n, mbstate_t* ps);
mbrlen() 函数检查第一个字节由s
指向的字符串 ,并为当前转换状态ps
确定其大小(以字节为单位)。检查s
最多n
个字节。
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