C语言转化编码格式(UTF8转GB2312)

// 示例:使用 MultiByteToWideChar 和 WideCharToMultiByte 进行编码转换
#include 
#include 

void convertUTF8ToGB2312(const char* utf8Str, char* gb2312Str, size_t gb2312Size) {
    // Convert UTF-8 to wide char
    int wstrLen = MultiByteToWideChar(CP_UTF8, 0, utf8Str, -1, NULL, 0);
    wchar_t* wstr = (wchar_t*)malloc(wstrLen * sizeof(wchar_t));
    MultiByteToWideChar(CP_UTF8, 0, utf8Str, -1, wstr, wstrLen);

    // Convert wide char to GB2312
    WideCharToMultiByte(CP_ACP, 0, wstr, -1, gb2312Str, gb2312Size, NULL, NULL);

    free(wstr);
}

你可能感兴趣的:(c语言,开发语言)