WindowsAPI在ANSI,UNICODE,UTF8编码之间转换

⌚Time: 2025-06-03 13:08:00

👨‍💻Author: Jack Ge

我让DeepSeek AI列举下面的字符编码转换函数

列举使用windowsapi的转换函数,具体是

1.ANSI转UNICODE
2.UNICODE转ANSI
3.UTF8转UNICODE
4.UNICODE转UTF8
5.UTF8转ANSI
6.ANSI转UTF8

列举转换函数

以下是使用 Windows API 进行各种字符编码转换的常用函数及示例代码(C/C++),涵盖你提到的所有转换场景:


核心函数说明

  1. MultiByteToWideChar
    • ANSI 或 UTF-8 转换为 Unicode(UTF-16)
  2. WideCharToMultiByte
    • Unicode(UTF-16) 转换为 ANSI 或 UTF-8

1. ANSI 转 Unicode(UTF-16)

#include <windows.h>

wchar_t* ANSItoUnicode(const char* ansiStr) {
    int len = MultiByteToWideChar(CP_ACP, 0, ansiStr, -1, NULL, 0);
    wchar_t* unicodeStr = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, ansiStr, -1, unicodeStr, len);
    return unicodeStr;
}

// 示例:
// const char* ansi = "中文";
// wchar_t* unicode = ANSItoUnicode(ansi);
// 记得用 delete[] unicode; 释放内存

2. Unicode(UTF-16)转 ANSI

char* UnicodeToANSI(const wchar_t* unicodeStr) {
    int len = WideCharToMultiByte(CP_ACP, 0, unicodeStr, -1, NULL, 0, NULL, NULL);
    char* ansiStr = new char[len];
    WideCharToMultiByte(CP_ACP, 0, unicodeStr, -1, ansiStr, len, NULL, NULL);
    return ansiStr;
}

// 示例:
// const wchar_t* unicode = L"中文";
// char* ansi = UnicodeToANSI(unicode);

3. UTF-8 转 Unicode(UTF-16)

wchar_t* UTF8toUnicode(const char* utf8Str) {
    int len = MultiByteToWideChar(CP_UTF8, 0, utf8Str, -1, NULL, 0);
    wchar_t* unicodeStr = new wchar_t[len];
    MultiByteToWideChar(CP_UTF8, 0, utf8Str, -1, unicodeStr, len);
    return unicodeStr;
}

// 示例:
// const char* utf8 = "\xE4\xB8\xAD\xE6\x96\x87"; // "中文"的UTF-8编码
// wchar_t* unicode = UTF8toUnicode(utf8);

4. Unicode(UTF-16)转 UTF-8

char* UnicodeToUTF8(const wchar_t* unicodeStr) {
    int len = WideCharToMultiByte(CP_UTF8, 0, unicodeStr, -1, NULL, 0, NULL, NULL);
    char* utf8Str = new char[len];
    WideCharToMultiByte(CP_UTF8, 0, unicodeStr, -1, utf8Str, len, NULL, NULL);
    return utf8Str;
}

// 示例:
// const wchar_t* unicode = L"中文";
// char* utf8 = UnicodeToUTF8(unicode);

5. UTF-8 转 ANSI

char* UTF8toANSI(const char* utf8Str) {
    // 先转Unicode,再转ANSI
    wchar_t* unicode = UTF8toUnicode(utf8Str);
    char* ansi = UnicodeToANSI(unicode);
    delete[] unicode;
    return ansi;
}

// 示例:
// const char* utf8 = "\xE4\xB8\xAD\xE6\x96\x87"; // "中文"的UTF-8
// char* ansi = UTF8toANSI(utf8);

6. ANSI 转 UTF-8

char* ANSItoUTF8(const char* ansiStr) {
    // 先转Unicode,再转UTF-8
    wchar_t* unicode = ANSItoUnicode(ansiStr);
    char* utf8 = UnicodeToUTF8(unicode);
    delete[] unicode;
    return utf8;
}

// 示例:
// const char* ansi = "中文";
// char* utf8 = ANSItoUTF8(ansi);

关键参数说明

参数 作用
CP_ACP 使用当前系统的ANSI代码页(如GBK、CP1252)。
CP_UTF8 指定UTF-8编码。
-1 自动计算输入字符串长度(以\0结尾)。
NULL, 0 查询所需缓冲区大小。

注意事项

  1. 内存管理:返回的字符串需用 delete[] 释放。
  2. 错误处理:检查返回值是否为0(失败时调用 GetLastError())。
  3. 编码兼容性:ANSI转换可能丢失信息(如UTF-8字符无法映射到ANSI时会替换为?)。

如果需要更安全的封装(如使用 std::wstring/std::string),可以进一步优化代码结构。

使用std::string或者std::wstring作为参数和返回值

以下是使用 Windows API 进行各种字符串编码转换的函数,参数使用 std::stringstd::wstring

1. ANSI 转 UNICODE (MultiByteToWideChar)

std::wstring AnsiToUnicode(const std::string& ansiStr, UINT codePage = CP_ACP)
{
    if (ansiStr.empty()) return L"";

    int length = MultiByteToWideChar(codePage, 0, ansiStr.c_str(), -1, nullptr, 0);
    if (length == 0) return L"";

    std::wstring unicodeStr(length, 0);
    MultiByteToWideChar(codePage, 0, ansiStr.c_str(), -1, &unicodeStr[0], length);

    // 去除末尾的null终止符
    unicodeStr.resize(length - 1);
    return unicodeStr;
}

2. UNICODE 转 ANSI (WideCharToMultiByte)

std::string UnicodeToAnsi(const std::wstring& unicodeStr, UINT codePage = CP_ACP)
{
    if (unicodeStr.empty()) return "";

    int length = WideCharToMultiByte(codePage, 0, unicodeStr.c_str(), -1, nullptr, 0, nullptr, nullptr);
    if (length == 0) return "";

    std::string ansiStr(length, 0);
    WideCharToMultiByte(codePage, 0, unicodeStr.c_str(), -1, &ansiStr[0], length, nullptr, nullptr);

    // 去除末尾的null终止符
    ansiStr.resize(length - 1);
    return ansiStr;
}

3. UTF-8 转 UNICODE

std::wstring Utf8ToUnicode(const std::string& utf8Str)
{
    return AnsiToUnicode(utf8Str, CP_UTF8);
}

4. UNICODE 转 UTF-8

std::string UnicodeToUtf8(const std::wstring& unicodeStr)
{
    return UnicodeToAnsi(unicodeStr, CP_UTF8);
}

5. UTF-8 转 ANSI

std::string Utf8ToAnsi(const std::string& utf8Str)
{
    std::wstring unicode = Utf8ToUnicode(utf8Str);
    return UnicodeToAnsi(unicode);
}

6. ANSI 转 UTF-8

std::string AnsiToUtf8(const std::string& ansiStr)
{
    std::wstring unicode = AnsiToUnicode(ansiStr);
    return UnicodeToUtf8(unicode);
}

使用示例

int main()
{
    // ANSI 转 UNICODE
    std::string ansi = "Hello ANSI";
    std::wstring unicode = AnsiToUnicode(ansi);
    
    // UNICODE 转 ANSI
    std::string backToAnsi = UnicodeToAnsi(unicode);
    
    // UTF-8 转 UNICODE
    std::string utf8 = u8"Hello UTF-8";
    std::wstring unicodeFromUtf8 = Utf8ToUnicode(utf8);
    
    // UNICODE 转 UTF-8
    std::string backToUtf8 = UnicodeToUtf8(unicodeFromUtf8);
    
    // UTF-8 转 ANSI
    std::string utf8ToAnsi = Utf8ToAnsi(utf8);
    
    // ANSI 转 UTF-8
    std::string ansiToUtf8 = AnsiToUtf8(ansi);
    
    return 0;
}

这些函数封装了 Windows API 的 MultiByteToWideCharWideCharToMultiByte 函数,提供了更方便的字符串转换方式,同时处理了内存分配和空字符串的情况。