C/C中的C32R()函数++

这个 C32R() 是C/C++中的一个内置函数,用于将32位字符表示转换为窄多字节字符表示。它是在 乌查尔。H C++的头文件。

null

语法 :

size_t c32rtomb(char* s, char32_t c32, mbstate_t* p)

参数 :该函数接受三个必需参数,如下所示:

  • s :指定存储多字节字符的字符串。
  • c16 :指定要转换的32位字符。
  • P :指定在解释多字节字符串时使用的mbstate_t对象的指针。

返回值 :该函数返回两个值,如下所示:

  • 程序成功后,函数返回写入s指向的字符数组的字节数。
  • 如果失败,则返回-1并 艾尔斯克 存储在错误号中。

方案1 :

// C++ program to illustrate the
// c32rtomb () function on it's success
#include <iostream>
#include <uchar.h>
#include <wchar.h>
using namespace std;
int main()
{
const char32_t str[] = U "GeeksforGeeks" ;
char s[50];
mbstate_t p{};
size_t length;
int j = 0;
while (str[j]) {
// initializing the function
length = c32rtomb(s, str[j], &p);
if ((length == 0) || (length > 50))
break ;
for ( int i = 0; i < length; ++i)
cout << s[i];
++j;
}
return 0;
}


输出:

GeeksforGeeks

方案2 :

// C++ program to illustrate the
// c32rtomb () function on it's failure
#include <iostream>
#include <uchar.h>
#include <wchar.h>
using namespace std;
int main()
{
const char32_t str[] = U "" ;
char s[50];
mbstate_t p{};
size_t length;
int j = 0;
while (str[j]) {
// initializing the function
length = c32rtomb(s, str[j], &p);
if ((length == 0) || (length > 50))
break ;
for ( int i = 0; i < length; ++i)
cout << s[i];
++j;
}
return 0;
}


输出:


注: 上述程序没有输出,因为这是一个失败的案例。

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享