C/C++ 中的 c16rtomb() 函式?


在C++中,可以使用16位字元表示。c16rtomb() 函式用於將16位字元表示轉換為窄多位元組字元表示。可以在 uchar.h 標頭檔案中找到此函式。

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

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

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

示例

 即時演示

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

輸出

Hello World

更新於: 30-7-2019

151 篇瀏覽

開啟您的 職業生涯

透過完成課程獲得證書

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