當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ mbrtowc()用法及代碼示例


C /C++中的mbrtowc()函數將多字節序列轉換為寬字符。此函數返回多字節字符的字節長度。 s指向的多字節字符將轉換為wchar_t類型的值,並存儲在pwc指向的位置。如果s指向空字符,則該函數將移位狀態複位並在將寬的空字符存儲在pwc之後返回零。

用法:

size_t mbrtowc (wchar_t* pwc, const char* pmb, size_t max, mbstate_t* ps)

參數:該函數接受四個參數,如下所述:


  • pwc :指向將寫入寬字符的位置的指針
  • s :指向用作輸入的多字節字符串的指針
  • n :可以檢查的s中的字節數限製
  • ps :指向解釋多字節字符串時使用的轉換狀態的指針

返回值:該函數返回四個值,如下所示:

  1. 如果為null寬字符或pmb為null指針,則該函數返回0
  2. 從s成功轉換的多字節字符的字節數[1…n]
  3. 如果pmb的最大前幾個字符形成不完整的多字節字符,則該函數返回length-2
  4. 否則,函數返回length-1並將errno設置為EILSEQ

注意:可能返回的值都不小於零。

以下示例程序旨在說明上述函數:
示例1:

// C++ program to illustrate 
// mbrtowc() function 
#include <bits/stdc++.h> 
using namespace std; 
  
// Function to convert multibyte 
// sequence to wide character 
void print_(const char* s) 
{ 
    // initial state 
    mbstate_t ps = mbstate_t(); 
  
    // length of the string 
    int length = strlen(s); 
  
    const char* n = s + length; 
    int len; 
    wchar_t pwc; 
  
    // printing each bytes 
    while ((len = mbrtowc(&pwc, s, n - s, &ps)) > 0) { 
        wcout << "Next " << len <<  
        " bytes are the character " << pwc << '\n'; 
        s += len; 
    } 
} 
  
// Driver code 
int main() 
{ 
    setlocale(LC_ALL, "en_US.utf8"); 
  
    // UTF-8 narrow multibyte encoding 
    const char* str = u8"z\u00df\u6c34\U0001d10b"; 
  
    print_(str); 
}
輸出:
Next 1 bytes are the character z
Next 2 bytes are the character Ã?
Next 3 bytes are the character æ°´
Next 4 bytes are the character ð??

示例2:

// C++ program to illustrate 
// mbrtowc() function 
// with different UTF-8 characters 
#include <bits/stdc++.h> 
using namespace std; 
  
// Function to convert multibyte 
// sequence to wide character 
void print_(const char* s) 
{ 
    // initial state 
    mbstate_t ps = mbstate_t(); 
  
    // length of the string 
    int length = strlen(s); 
  
    const char* n = s + length; 
    int len; 
    wchar_t pwc; 
  
    // printing each bytes 
    while ((len = mbrtowc(&pwc, s, n - s, &ps)) > 0) { 
        wcout << "Next " << len <<  
        " bytes are the character " << pwc << '\n'; 
        s += len; 
    } 
} 
  
// Driver code 
int main() 
{ 
    setlocale(LC_ALL, "en_US.utf8"); 
  
    // UTF-8 narrow multibyte encoding 
    const char* str = u8"\xE2\x88\x83y\xE2\x88\x80x\xC2\xAC"; 
  
    print_(str); 
}
輸出:
Next 3 bytes are the character â??
Next 1 bytes are the character y
Next 3 bytes are the character â??
Next 1 bytes are the character x
Next 2 bytes are the character ¬


相關用法


注:本文由純淨天空篩選整理自AmanSrivastava1大神的英文原創作品 mbrtowc() function in C/C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。