首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C語言實現GBK、GB2312、Big5編碼互轉

     編輯:關於C語言

概述

本文提供四個基於 Windows API 的函數,實現簡體中文(GBK/GB2312)與繁體中文(Big5)之間的編碼轉換。核心思路是利用 MultiByteToWideChar 將多位元組字符集轉為 Unicode(宽字符),再用 WideCharToMultiByte 轉為目標編碼;GB2312 與 GBK 間則使用 LCMapString 進行繁簡轉換。

核心函數

1. Big5 轉 GBK

void BIG52GBK(char *szBuf) {
    if (!strcmp(szBuf, "")) return;
    int nStrLen = strlen(szBuf);
    wchar_t *pws = new wchar_t[nStrLen + 1];
    try {
        int nReturn = MultiByteToWideChar(950, 0, szBuf, nStrLen, pws, nStrLen + 1);
        BOOL bValue = false;
        nReturn = WideCharToMultiByte(936, 0, pws, nReturn, szBuf, nStrLen + 1, "?", &bValue);
        szBuf[nReturn] = 0;
    } __finally {
        delete[] pws;
    }
}

2. GBK 轉 Big5

void GBK2BIG5(char *szBuf) {
    if (!strcmp(szBuf, "")) return;
    int nStrLen = strlen(szBuf);
    wchar_t *pws = new wchar_t[nStrLen + 1];
    try {
        MultiByteToWideChar(936, 0, szBuf, nStrLen, pws, nStrLen + 1);
        BOOL bValue = false;
        WideCharToMultiByte(950, 0, pws, nStrLen, szBuf, nStrLen + 1, "?", &bValue);
        szBuf[nStrLen] = 0;
    } __finally {
        delete[] pws;
    }
}

3. GB2312 轉 GBK(繁轉簡)

void GB2GBK(char *szBuf) {
    if (!strcmp(szBuf, "")) return;
    int nStrLen = strlen(szBuf);
    WORD wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_PRC);
    int nReturn = LCMapString(wLCID, LCMAP_TRADITIONAL_CHINESE, szBuf, nStrLen, NULL, 0);
    if (!nReturn) return;
    char *pcBuf = new char[nReturn + 1];
    try {
        wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_PRC);
        LCMapString(wLCID, LCMAP_TRADITIONAL_CHINESE, szBuf, nReturn, pcBuf, nReturn + 1);
        strncpy(szBuf, pcBuf, nReturn);
    } __finally {
        delete[] pcBuf;
    }
}

4. GBK 轉 GB2312(簡轉繁)

void GBK2GB(char *szBuf) {
    if (!strcmp(szBuf, "")) return;
    int nStrLen = strlen(szBuf);
    WORD wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_BIG5);
    int nReturn = LCMapString(wLCID, LCMAP_SIMPLIFIED_CHINESE, szBuf, nStrLen, NULL, 0);
    if (!nReturn) return;
    char *pcBuf = new char[nReturn + 1];
    try {
        wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_BIG5);
        LCMapString(wLCID, LCMAP_SIMPLIFIED_CHINESE, szBuf, nReturn, pcBuf, nReturn + 1);
        strncpy(szBuf, pcBuf, nReturn);
    } __finally {
        delete[] pcBuf;
    }
}

測試示例

void TestConversion() {
    char szBuf[255];
    // GB2312 -> GBK
    strcpy(szBuf, "測試字符串");
    GB2GBK(szBuf);
    // GB2312 -> Big5 (經 GBK 中轉)
    strcpy(szBuf, "測試字符串");
    GB2GBK(szBuf);
    GBK2BIG5(szBuf);
}

關鍵點說明

  • 代碼頁標識:936 = GBK,950 = Big5。
  • LCMapString 的 LCMAP_TRADITIONAL_CHINESE 為簡轉繁,LCMAP_SIMPLIFIED_CHINESE 為繁轉簡。
  • 所有函數均在原緩衝區上修改字符串,調用者需確保緩衝區足夠大。
  • 此實現依賴 Windows 平台 API,跨平台項目建議使用 ICU 或 iconv 庫。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved