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