4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

標準関数でstd::wstring(UTF-16)↔std::string(UTF-8)変換

Last updated at Posted at 2022-08-07

はじめに

マルチプラットフォームで実現するため、標準関数で変換する。
ただし、最近のC++では非推奨となっています。

C++ バージョン C++11 C++17で非推奨

共通

インクルードファイルは共通です。

#include <string>
#include <locale>
#include <codecvt>

std::wstring⇒std::string変換

static std::string ConvertWstringToUTF8(const std::wstring& src)
{
	std::wstring_convert<std::codecvt_utf8<wchar_t> > converter;
	return converter.to_bytes(src);
}

std::string⇒std::wstring変換

static std::wstring ConvertUTF8ToWstring(const std::string& src)
{
	std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
	return converter.from_bytes(src);
}
4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?