博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
调用Windows API实现GBK和UTF-8的相互转换
阅读量:6324 次
发布时间:2019-06-22

本文共 5294 字,大约阅读时间需要 17 分钟。

GBK转UTF-8示例

GbkToUtf8.cpp

#include 
#include
#include
#include
int main(){ using namespace std; string multiByteString = "我25岁。\nI'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 
#include
#define BUFFER_SIZE 1000int 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] = '\0'; 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\n", outputFilename); } else { printf("Cannot write file: %s\n", outputFilename); } free(gbkString); free(unicodeString); fclose(inputFile); } else { printf("Cannot read file: %s\n", inputFilename); } system("pause"); return 0;}

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

EncodingConverter.h

#pragma once#include 
#include
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 
#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;}

 

转载于:https://www.cnblogs.com/buyishi/p/10396919.html

你可能感兴趣的文章
最短路径算法实现
查看>>
基于Netty 实现简单的私有协议
查看>>
SpringBoot集成Swagger接口管理工具
查看>>
[转]结队编程——软件测试报告 10061178 刘宇翔 10061148 彭笑东
查看>>
iOS 多线程的使用
查看>>
iOS 自动布局详细介绍
查看>>
wordpress后台进去空白怎么办?
查看>>
WCF
查看>>
linux redis命令
查看>>
BZOJ4764弹飞大爷——LCT
查看>>
[Unity] 2D开发学习教程
查看>>
css样式表--样式表分类
查看>>
网络爬虫之框架(Scrapy)
查看>>
Thumbnailst图片压缩处理
查看>>
1.8站立会议
查看>>
mysql 插入超过1M字符串内容时,失败的解决办法
查看>>
继承关系下的this关键字
查看>>
[oracle]Oracle 11g 初体验(1)
查看>>
putty 蓝色字体 看不清
查看>>
[20171110]_allow_read_only_corruption参数.txt
查看>>