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

这个 wcrtomb() C/C++中的函数将宽字符转换为其窄多字节表示形式。宽字符 厕所 转换为其多字节等效值,并存储在 s .该函数返回以字节为单位的等效多字节序列的长度 s .

null

语法:

size_t wcrtomb( char* s, wchar_t wc, mbstate_t* ps )

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

  • s: 指定指向足以容纳多字节序列的数组的指针
  • 厕所: 指定要转换的宽字符。
  • 附言: 指定解释多字节字符串时使用的转换状态指针

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

  • 成功时,它返回写入字符数组的字节数,该字符数组的第一个元素由s指向。
  • 否则,它返回-1,errno设置为 艾尔斯克 .

以下程序说明了上述功能: 项目1:

// C++ program to illustrate the
// wcrtomb() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
setlocale (LC_ALL, "en_US.utf8" );
// initialize the string
wchar_t wc[] = L "zu00dfu6c34U0001f34c" ;
// array large enough to hold a multibyte sequence
char s[25];
int returnV;
// initial state
mbstate_t ps = mbstate_t ();
for ( int i = 0; i < wcslen(wc); i++) {
returnV = wcrtomb(s, wc[i], &ps);
// print byte size, if its a valid character
if (returnV != -1)
cout << "Size of " << s << " is "
<< returnV << " bytes" << endl;
else
cout << "Invalid wide character" << endl;
}
return 0;
}


输出:

Size of z is 1 bytes
Size of Ã? is 2 bytes
Size of æ°´ is 3 bytes
Size of ð?? is 4 bytes

项目2:

// C++ program to illustrate the
// wcrtomb() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
setlocale (LC_ALL, "en_US.utf8" );
// initialize the string
wchar_t wc[] = L "uu00c6u00f5u01b5" ;
// array large enough to hold a multibyte sequence
char s[20];
int returnV;
// initial state
mbstate_t ps = mbstate_t ();
for ( int i = 0; i < wcslen(wc); i++) {
returnV = wcrtomb(s, wc[i], &ps);
// print byte size, if its a valid character
if (returnV != -1)
cout << "Size of " << s << " is "
<< returnV << " bytes" << endl;
else
cout << "Invalid wide character" << endl;
}
return 0;
}


输出:

Size of u    Ì_e is 1 bytes
Size of Ã?Ì_e is 2 bytes
Size of õÌ_e is 2 bytes
Size of ƵÌ_e is 2 bytes

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