이 mbrtowc() 함수는 멀티바이트 시퀀스를 와이드 문자열로 변환하는 데 사용됩니다. 멀티바이트 문자의 길이를 바이트 단위로 반환합니다. 구문은 아래와 같습니다.
mbrtowc (wchar_t* wc, const char* s, size_t max, mbstate_t* ps)
인수는 -
- wc는 결과 와이드 문자가 저장될 위치를 가리키는 포인터입니다.
- 입력으로 멀티바이트 문자열에 대한 포인터입니다.
- 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 क