यह 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(); // प्रारंभिक स्थिति
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;
}
}
int main() {
setlocale(LC_ALL, "en_US.utf8");
const char* str = u8"z\u00cf\u7c38\U00000915";
display(str);
return 0;
}
आउटपुट
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 क
यह फंक्शन UTF-8 जैसे मल्टीबाइट एन्कोडिंग को वाइड कैरेक्टर्स (wchar_t) में बदलने के लिए आवश्यक है। यह सही ढंग से मल्टीबाइट सीक्वेंस को पार्स करता है और प्रत्येक कैरेक्टर की बाइट लंबाई बताता है। setlocale और UTF-8 लिटरल्स (u8 prefix) का उपयोग सही संचालन के लिए महत्वपूर्ण है।