nprogram’s blog

気ままに、プログラミングのトピックについて書いていきます

UnicodeからUTF-8に変換する方法 [C++] [MFC]

はじめに

UnicodeからUTF-8に変換する方法を調べたので、記録します。

Convert Unicode (utf-16) CStringW to utf-8 CStringA

CStringA ConvertUnicodeToUTF8(const CStringW& uni)
{
    if (uni.IsEmpty()) return ""; // nothing to do
    CStringA utf8;
    int cc=0;
    // get length (cc) of the new multibyte string excluding the \0 terminator first
    if ((cc = WideCharToMultiByte(CP_UTF8, 0, uni, -1, NULL, 0, 0, 0) - 1) > 0)
    { 
        // convert
        char *buf = utf8.GetBuffer(cc);
        if (buf) WideCharToMultiByte(CP_UTF8, 0, uni, -1, buf, cc, 0, 0);
        utf8.ReleaseBuffer();
    }
    return utf8;
}

Convert utf-8 CStringA to Unicode (utf-16) CStringW

CStringW ConvertUTF8ToUnicode(const CStringA& utf8)
{
    if (utf8.IsEmpty()) return L""; // nothing to do
    CStringW uni;
    int cc=0;
    // get length (cc) of the new widechar excluding the \0 terminator first
    if ((cc = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0) - 1) > 0)
    { 
        // convert
        wchar_t *buf = uni.GetBuffer(cc);
        if (buf) MultiByteToWideChar(CP_UTF8, 0, utf8, -1, buf, cc);
        uni.ReleaseBuffer();
    }
    return uni;
}

参考リンク

Convert Unicode (utf-16) CString to utf-8 and reverse