본문 바로가기

카테고리 없음

어떤 문자열이든 utf8로 바꿔주는 코드

#include <windows.h>
#include <string>

std::string to_utf8(const std::wstring& wstr) {
    int size = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
    std::string result(size - 1, 0);
    WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, result.data(), size, nullptr, nullptr);
    return result;
}