当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ c32rtomb()用法及代码示例


c32rtomb()是C /C++中的内置函数,它将32位字符表示形式转换为窄的多字节字符表示形式。它在C++的uchar.h头文件中定义。

用法

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

参数:该函数接受三个强制性参数,如下所示:


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

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

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

程序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; 
}
输出:

注意:由于是失败的情况,因此上述程序中没有输出。



相关用法


注:本文由纯净天空筛选整理自IshwarGupta大神的英文原创作品 c32rtomb() function in C/C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。