Skip to content

Instantly share code, notes, and snippets.

@bcachet
Last active November 2, 2021 08:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bcachet/96b762b5d2bad6d2eff2 to your computer and use it in GitHub Desktop.
Save bcachet/96b762b5d2bad6d2eff2 to your computer and use it in GitHub Desktop.
Convert string to wstring to write to file with utf-16 encoding
// Windows version
#include <string>
#include <iostream>
#include <fstream>
#include <locale>
#include <codecvt>
#include <Windows.h>
std::wstring decode(const std::string &str, int codePage = GetACP())
{
if (str.empty()) return std::wstring();
int size_needed = MultiByteToWideChar(codePage, 0, &str[0], (int)str.size(), NULL, 0);
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(codePage, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
return wstrTo;
}
std::string encode(const std::wstring &wstr, int codePage = GetACP())
{
if (wstr.empty()) return std::string();
int size_needed = WideCharToMultiByte(codePage, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string strTo(size_needed, 0);
WideCharToMultiByte(codePage, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
return strTo;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::wofstream fout("text-utf16.txt", std::ios::binary);
const unsigned long MaxCode = 0x10ffff;
const std::codecvt_mode Mode = (std::codecvt_mode)(std::generate_header | std::little_endian);
std::locale utf16_locale(fout.getloc(), new std::codecvt_utf16<wchar_t, MaxCode, Mode>);
fout.imbue(utf16_locale);
fout << decode("ßz水");
// You can also encode to other codePage
std::ofstream fout2("text-858.txt", std::ios::binary);
fout2 << encode(decode("ßz水"));
}
// Linux Version
#include <string>
#include <iostream>
#include <fstream>
#include <locale>
#include <codecvt>
int main() {
char s[] = "ßz水";
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring wide = converter.from_bytes(s);
std::wofstream fout("text.txt", std::ios::binary);
const unsigned long MaxCode = 0x10ffff;
const std::codecvt_mode Mode = std::generate_header;
std::locale utf16_locale(fout.getloc(), new std::codecvt_utf16<wchar_t, MaxCode, Mode>);
fout.imbue(utf16_locale);
fout << wide;
}
@bcachet
Copy link
Author

bcachet commented Nov 26, 2015

Windows Version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment