这个 mbrtoc16() 是C/C++中的一个内置函数,用于将窄多字节字符转换为16位字符表示形式。它是在 乌查尔。H C++的头文件。
null
语法 :
ssize_t mbrtoc16( char16_t* pc16, const char* s, size_t n, mbstate_t* ps);
参数 :该函数接受四个强制参数,如下所述:
- s :指定要转换的多字节字符。
- pc16 :指定存储结果16位字符的内存位置。
- N :指定要转换的最大字节数(以秒为单位)。
- 附言 :指定在解释多字节字符串时使用的mbstate_t对象。
返回值 :函数返回五个值,如下所示:
- 如果转换的字符为空字符,则为0。
- 成功转换为16位字符的多字节字符的字节数(最多n)。
- -3如果多字符(例如代理项对)中的下一个字符已写入*pc16。在这种情况下,不会处理来自输入的字节。
- -2如果接下来的n个字节构成了一个不完整但迄今为止有效的多字节字符。在这种情况下,不会向*pc16写入任何内容。
- -1如果发生编码错误。在这种情况下,不会向*pc16写入任何内容,errno设置为 艾尔斯克 *ps的值未指定。
下面的程序说明了上述功能。
方案1 :
// C++ program to illustrate the // mbrtoc16() function #include <cstdio> #include <cstdlib> #include <iostream> #include <uchar.h> #include <wchar.h> using namespace std; int main( void ) { char16_t pc16; char s[] = "G" ; mbstate_t ps{}; int length; // initializing the function length = mbrtoc16(&pc16, s, MB_CUR_MAX, &ps); if (length < 0) { perror ( "mbrtoc16() fails to convert" ); exit (-1); } cout << "Multibyte string = " << s << endl; cout << "Length = " << length << endl; printf ( "16-bit character = 0g%02hd" , pc16); } |
输出:
Multibyte string = G Length = 1 16-bit character = 0g71
方案2 :
// C++ program to illustrate the // mbrtoc16() function #include <cstdio> #include <cstdlib> #include <iostream> #include <uchar.h> #include <wchar.h> using namespace std; int main( void ) { char16_t pc16; char s[] = "" ; mbstate_t ps{}; int length; // initializing the function length = mbrtoc16(&pc16, s, MB_CUR_MAX, &ps); if (length < 0) { perror ( "mbrtoc16() fails to convert" ); exit (-1); } cout << "Multibyte string = " << s << endl; cout << "Length = " << length << endl; printf ( "16-bit character = 1e%04xy" , pc16); } |
输出:
Multibyte string = Length = 0 16-bit character = 1e0000y
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END