C/C++語言中的c32rtomb()函式?


在C ++語言中,我們可以使用 32 位字元表示。c32rtomb() 函式用於將 32 位字元表示轉換成窄的、多位元組字元表示。我們可以在 uchar.h 標頭檔案中找到此函式。

此函式接受三個引數。它們是:

  • 儲存多位元組字元的字串
  • 要轉換的 32 位字元
  • 型別為 mbstate_t 物件的指標。該指標用於解釋多位元組字串。

此函式返回寫入字元陣列的位元組數,如果成功則返回 -1。讓我們看一個示例以更好地理解。

示例

 線上示例

#include <iostream>
#include <uchar.h>
#include <wchar.h>
using namespace std;
int main() {
   const char32_t myStr[] = U"Hello World";
   char dest[50];
   mbstate_t p{};
   size_t length;
   int j = 0;
   while (myStr[j]) {
      length = c32rtomb(dest, myStr[j], &p); //get length from c32rtomb() method
      if ((length == 0) || (length > 50))
         break;
      for (int i = 0; i < length; ++i)
         cout << dest[i];
         j++;
   }
}

輸出

Hello World

更新於:30-Jul-2019

113 次瀏覽

開啟你的 職業生涯

完成本課程,並獲得認證

開始學習
廣告
© . All rights reserved.