C/C++ 中的 mbrtowc() 函数
此 mbrtowc() 函数用于将多字节序列转换为宽字符字符串。它以字节返回多字节字符的长度。语法如下所示。
mbrtowc (wchar_t* wc, const char* s, size_t max, mbstate_t* ps)
参数如下 −
- wc 指向存储结果宽字符的位置。
- s 是输入的多字节字符字符串的指针
- max 是 s 中可检查的最大字节数
- ps 指向转换状态,用于解释多字节字符串。
示例
#include <bits/stdc++.h>
using namespace std;
void display(const char* s) {
mbstate_t ps = mbstate_t(); // initial state
int s_len = strlen(s);
const char* n = s + s_len;
int len;
wchar_t wide_char;
while ((len = mbrtowc(&wide_char, s, n - s, &ps)) > 0) {
wcout << "The following " << len << " bytes are for the character " << wide_char << '\n';
s += len;
}
}
main() {
setlocale(LC_ALL, "en_US.utf8");
const char* str = u8"z\u00cf\u7c38\U00000915";
display(str);
}输出
The following 1 bytes are for the character z The following 2 bytes are for the character Ï The following 3 bytes are for the character 簸 The following 3 bytes are for the character क
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP