zoukankan      html  css  js  c++  java
  • 调用Windows API实现GBK和UTF-8的相互转换

    GBK转UTF-8示例

    GbkToUtf8.cpp

    #include <Windows.h>
    #include <iostream>
    #include <string>
    #include <fstream>
    int main()
    {
        using namespace std;
        string multiByteString = "我25岁。
    I'm 25 years old.";
        int bufferSize = MultiByteToWideChar(CP_ACP, 0, multiByteString.c_str(), -1, nullptr, 0);
        WCHAR *unicodeString = new WCHAR[bufferSize];
        MultiByteToWideChar(CP_ACP, 0, multiByteString.c_str(), -1, unicodeString, bufferSize);
        bufferSize = WideCharToMultiByte(CP_UTF8, 0, unicodeString, -1, nullptr, 0, nullptr, nullptr);
        CHAR *utf8String = new CHAR[bufferSize];
        WideCharToMultiByte(CP_UTF8, 0, unicodeString, -1, utf8String, bufferSize, nullptr, nullptr);
        ofstream ofs("UTF8.txt");
        if (ofs)
        {
            ofs.write(utf8String, bufferSize - 1);
            cout << "A UTF-8 string has been written to file: UTF8.txt" << endl;
        }
        else
        {
            cout << "Cannot create file: UTF8.txt" << endl;
        }
        delete[] utf8String;
        delete[] unicodeString;
        system("pause");
        return 0;
    }

    UTF-8转GBK示例

    Utf8ToGbk.c

    #include <Windows.h>
    #include <stdio.h>
    #define BUFFER_SIZE 1000
    int main()
    {
        const char *inputFilename = "Utf8Text.txt";
        FILE *inputFile = fopen(inputFilename, "r");
        if (inputFile)
        {
            char utf8Text[BUFFER_SIZE];
            size_t numberOfObjectsRead = fread(utf8Text, sizeof(char), BUFFER_SIZE, inputFile);
            utf8Text[numberOfObjectsRead] = '';
            int bufferSize = MultiByteToWideChar(CP_UTF8, 0, utf8Text, -1, NULL, 0);
            WCHAR *unicodeString = (WCHAR *)malloc(sizeof(WCHAR) * bufferSize);
            MultiByteToWideChar(CP_UTF8, 0, utf8Text, -1, unicodeString, bufferSize);
            bufferSize = WideCharToMultiByte(CP_ACP, 0, unicodeString, -1, NULL, 0, NULL, NULL);
            CHAR *gbkString = (CHAR *)malloc(sizeof(CHAR) * bufferSize);
            WideCharToMultiByte(CP_ACP, 0, unicodeString, -1, gbkString, bufferSize, NULL, NULL);
            const char *outputFilename = "GbkText.txt";
            FILE *outputFile = fopen(outputFilename, "w");
            if (outputFile)
            {
                fwrite(gbkString, sizeof(CHAR), bufferSize - 1, outputFile);
                fclose(outputFile);
                printf("The GBK text has been written to file: %s
    ", outputFilename);
            }
            else
            {
                printf("Cannot write file: %s
    ", outputFilename);
            }
            free(gbkString);
            free(unicodeString);
            fclose(inputFile);
        }
        else
        {
            printf("Cannot read file: %s
    ", inputFilename);
        }
        system("pause");
        return 0;
    }

    以下是我对转换过程的封装

    EncodingConverter.h

    #pragma once
    #include <Windows.h>
    #include <string>
    class EncodingConverter
    {
    public:
        EncodingConverter(UINT fromCodePage, UINT toCodePage);
        std::string convert(const std::string &from) const;
        static std::wstring convertToUnicode(UINT fromCodePage, const std::string &from);
        static std::string unicodeConvertTo(UINT toCodePage, const std::wstring &from);
    private:
        UINT fromCodePage;
        UINT toCodePage;
    };

    EncodingConverter.cpp

    #include "EncodingConverter.h"
    EncodingConverter::EncodingConverter(UINT fromCodePage, UINT toCodePage) : fromCodePage(fromCodePage), toCodePage(toCodePage) { }
    std::string EncodingConverter::convert(const std::string &from) const
    {
        int bufferSize = MultiByteToWideChar(fromCodePage, 0, from.c_str(), -1, nullptr, 0);
        WCHAR *unicodeString = new WCHAR[bufferSize];
        MultiByteToWideChar(fromCodePage, 0, from.c_str(), -1, unicodeString, bufferSize);
        bufferSize = WideCharToMultiByte(toCodePage, 0, unicodeString, -1, nullptr, 0, nullptr, nullptr);
        CHAR *to = new CHAR[bufferSize];
        WideCharToMultiByte(toCodePage, 0, unicodeString, -1, to, bufferSize, nullptr, nullptr);
        std::string toString(to);
        delete[] to;
        delete[] unicodeString;
        return toString;
    }
    std::wstring EncodingConverter::convertToUnicode(UINT fromCodePage, const std::string &from)
    {
        int bufferSize = MultiByteToWideChar(fromCodePage, 0, from.c_str(), -1, nullptr, 0);
        WCHAR *unicodeString = new WCHAR[bufferSize];
        MultiByteToWideChar(fromCodePage, 0, from.c_str(), -1, unicodeString, bufferSize);
        std::wstring toString(unicodeString);
        delete[] unicodeString;
        return toString;
    }
    std::string EncodingConverter::unicodeConvertTo(UINT toCodePage, const std::wstring &from)
    {
        int bufferSize = WideCharToMultiByte(toCodePage, 0, from.c_str(), -1, nullptr, 0, nullptr, nullptr);
        CHAR *to = new CHAR[bufferSize];
        WideCharToMultiByte(toCodePage, 0, from.c_str(), -1, to, bufferSize, nullptr, nullptr);
        std::string toString(to);
        delete[] to;
        return toString;
    }

    EncodingConversionDemo.cpp

    #include <iostream>
    #include "EncodingConverter.h"
    using namespace std;
    int main()
    {
        const string &utf8String = EncodingConverter(CP_ACP, CP_UTF8).convert("Are you OK? -- 你还好吗");
        cout << utf8String << endl;
        const string &gbkString = EncodingConverter(CP_UTF8, CP_ACP).convert("浣犺繕濂藉悧");
        cout << gbkString << endl;
        const wstring &unicodeString = EncodingConverter::convertToUnicode(CP_UTF8, "浣犺繕濂藉悧");
        wcout.imbue(locale("chs"));
        wcout << unicodeString << endl;
        cout << EncodingConverter::unicodeConvertTo(CP_ACP, wstring(L"别笑青蛙没有见过大海,在河边一样可以自由自在。")) << endl;
        system("pause");
        return 0;
    }
  • 相关阅读:
    nginx构建https
    安装mysql的遇到的问题
    docker mysql 2059
    centos7 安装chrome
    谷歌浏览器启动参数
    php-webdriver:PHP控制浏览器动作, php web驱动, PHP实现自动化, php webdriver 教程, A php client for webdriver.
    nginx转发
    nginx变量与实列
    mysql 语句
    frp 内网穿透配置
  • 原文地址:https://www.cnblogs.com/buyishi/p/10396919.html
Copyright © 2011-2022 走看看