最終更新日時:
が更新

履歴 編集

function
<cwchar>

std::mbrtowc

namespace std {
  size_t mbrtowc(wchar_t* pwc, const char* s, size_t n, mbstate_t* ps);
}

概要

マルチバイト文字を、変換状態を保持しながらワイド文字へ変換する。

効果

sが指す位置から最大nバイトを読み込み、1文字分のマルチバイト文字をワイド文字へ変換して、pwcが非nullptrであればそこへ格納する。

戻り値

戻り値 意味
0 ヌル文字へ変換された
正の値(n以下) 変換に使用したバイト数
(size_t)-1 不正なバイト列である。errnoEILSEQが設定される
(size_t)-2 次のnバイトは不完全だが、不正ではない文字の一部である

備考

  • sがヌルポインタの場合、pwcnは無視され、変換状態を初期状態に戻す動作となる
  • psがヌルポインタの場合、処理系が用意した内部のオブジェクトが変換状態として使用される

#include <cwchar>
#include <clocale>
#include <iostream>

int main()
{
  std::setlocale(LC_ALL, "C.UTF-8");

  std::mbstate_t state{};
  const char* s = "あ";
  wchar_t wc = 0;

  // UTF-8では3バイトを消費して1文字へ変換される
  std::size_t n = std::mbrtowc(&wc, s, 4, &state);
  std::wcout << n << std::endl;
  std::wcout << (wc == L'あ') << std::endl;
}

出力例

3
1

バージョン

言語

  • C++98

関連項目