GBK转UTF-8示例

GbkToUtf8.cpp

#include <Windows.h>
#include <iostream>
#include <string>
#include <fstream>
int main()
{
using namespace std;
string multiByteString = "我25岁。\nI'm 25 years old.";
int bufferSize = MultiByteToWideChar(CP_ACP, , multiByteString.c_str(), -, nullptr, );
WCHAR *unicodeString = new WCHAR[bufferSize];
MultiByteToWideChar(CP_ACP, , multiByteString.c_str(), -, unicodeString, bufferSize);
bufferSize = WideCharToMultiByte(CP_UTF8, , unicodeString, -, nullptr, , nullptr, nullptr);
CHAR *utf8String = new CHAR[bufferSize];
WideCharToMultiByte(CP_UTF8, , unicodeString, -, utf8String, bufferSize, nullptr, nullptr);
ofstream ofs("UTF8.txt");
if (ofs)
{
ofs.write(utf8String, bufferSize - );
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 ;
}

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] = '\0';
int bufferSize = MultiByteToWideChar(CP_UTF8, , utf8Text, -, NULL, );
WCHAR *unicodeString = (WCHAR *)malloc(sizeof(WCHAR) * bufferSize);
MultiByteToWideChar(CP_UTF8, , utf8Text, -, unicodeString, bufferSize);
bufferSize = WideCharToMultiByte(CP_ACP, , unicodeString, -, NULL, , NULL, NULL);
CHAR *gbkString = (CHAR *)malloc(sizeof(CHAR) * bufferSize);
WideCharToMultiByte(CP_ACP, , unicodeString, -, gbkString, bufferSize, NULL, NULL);
const char *outputFilename = "GbkText.txt";
FILE *outputFile = fopen(outputFilename, "w");
if (outputFile)
{
fwrite(gbkString, sizeof(CHAR), bufferSize - , 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 ;
}

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

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, , from.c_str(), -, nullptr, );
WCHAR *unicodeString = new WCHAR[bufferSize];
MultiByteToWideChar(fromCodePage, , from.c_str(), -, unicodeString, bufferSize);
bufferSize = WideCharToMultiByte(toCodePage, , unicodeString, -, nullptr, , nullptr, nullptr);
CHAR *to = new CHAR[bufferSize];
WideCharToMultiByte(toCodePage, , unicodeString, -, 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, , from.c_str(), -, nullptr, );
WCHAR *unicodeString = new WCHAR[bufferSize];
MultiByteToWideChar(fromCodePage, , from.c_str(), -, unicodeString, bufferSize);
std::wstring toString(unicodeString);
delete[] unicodeString;
return toString;
}
std::string EncodingConverter::unicodeConvertTo(UINT toCodePage, const std::wstring &from)
{
int bufferSize = WideCharToMultiByte(toCodePage, , from.c_str(), -, nullptr, , nullptr, nullptr);
CHAR *to = new CHAR[bufferSize];
WideCharToMultiByte(toCodePage, , from.c_str(), -, 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 ;
}

调用Windows API实现GBK和UTF-8的相互转换的更多相关文章

  1. C#调用windows API的一些方法

    使用C#调用windows API(从其它地方总结来的,以备查询) C#调用windows API也可以叫做C#如何直接调用非托管代码,通常有2种方法: 1.  直接调用从 DLL 导出的函数. 2. ...

  2. C#调用Windows API函数截图

    界面如下: 下面放了一个PictureBox 首先是声明函数: //这里是调用 Windows API函数来进行截图 //首先导入库文件 [System.Runtime.InteropServices ...

  3. 【转】用C#调用Windows API向指定窗口发送

    一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.InteropServices; 2.引用需要使用的方法,格式 ...

  4. C#中调用Windows API的要点 .

    介绍 API(Application Programming Interface),我想大家不会陌生,它是我们Windows编程的常客,虽然基于.Net平台的C#有了强大的类库,但是,我们还是不能否认 ...

  5. c# 判断窗体是否永在最前(TopMost),调用windows API

    许多程序都可以把自身的窗体设为最前显示状态,这个可以参考博客c#让窗体永在最前 调用windows api 将窗体设为topmost.那么如何判断桌面上的一个窗体是否为最前显示状态呢,不光是自己的程序 ...

  6. c#让窗体永在最前 调用windows api 将窗体设为topmost

    有时候应用程序需要将一个窗体始终位于屏幕的最前面,即使切换到其它窗体也能看到该窗体,这样的窗体就叫做TopMost窗体. 用C#制作TopMost窗体之前,首先要了解如何声明SetWindowPos函 ...

  7. 善于 调用Windows API

    前一段时间看见别人做的一个自动填写信息并且点击登录的程序,觉得很有意思. 其实就是在程序中调用Windows的API,那么如何调用,下面就做个简单的介绍. 写的简单粗暴, 不喜轻喷. 0.首先引入名称 ...

  8. C#中调用Windows API时的数据类型对应关系

    原文 C#中调用Windows API时的数据类型对应关系 BOOL=System.Int32 BOOLEAN=System.Int32 BYTE=System.UInt16 CHAR=System. ...

  9. 用C#调用Windows API向指定窗口发送按键消息 z

    用C#调用Windows API向指定窗口发送 一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.Interop ...

随机推荐

  1. django:访问本地静态文件的配置

    1.在setting.py中新增如下配置,static为静态文件的目录,BASE_DIR为项目根目录 STATIC_URL = '/static/' STATIC_ROOT = os.path.joi ...

  2. 使用 IntelliJ IDEA 开发 Android 应用程序时配置 Allatori 进行代码混淆

    IntelliJ IDEA 提供了非常强大的 Android 开发支持,就连 Google 官方推荐的 Android Studio 其实也是 IntelliJ IDEA 的一个 Android 开发 ...

  3. AC日记——王室联邦 bzoj 1086

    Description “余”人国的国王想重新编制他的国家.他想把他的国家划分成若干个省,每个省都由他们王室联邦的一个成员来管理.他的国家有n个城市,编号为1..n.一些城市之间有道路相连,任意两个不 ...

  4. Network | router & switch

    路由器 A router is a device that forwards data packets between computer networks. This creates an overl ...

  5. Arduino可穿戴教程保存源文件与打开已经存在的源文件

    Arduino可穿戴教程保存源文件与打开已经存在的源文件 Arduino IDE保存源文件 保存源文件可以通过“文件”菜单的“保存”或者快捷键Ctrl+S完成,如图2.28所示.   图2.28  保 ...

  6. Play框架的用户验证。

    最近刚刚参与一个基于Play框架的管理平台的升级工作,其中涉及到了用户的验证工作.第一次接触play框架,直接看已有代码,有点晕.因此,自己实现了一个简单的用户验证功能. 首先,新建一个User类,包 ...

  7. link2005 重复定义错误

    造成LNK2005错误主要有以下几种情况:  1.重复定义全局变量. 对于一些初学编程的程序员,有时候会以为需要使用全局变量的地方就可以使用定义申明一下.其实这是错误的,全局变量是针对整个工程的. 正 ...

  8. BT网络中DHT和UPnp的解释(转)

    DHT 类似Tracker的根据种子特征码返回种子信息的网络.DHT全称叫分布式哈希表(Distributed Hash Table),是一种分布式存储方法.在不需要服务器的情况下,每个客户端负责一个 ...

  9. javascript 对象初探 (六)--- call()和apply()初探

    在javascript中,每个函数都具有call()和apply()两个方法,您可以用她们来触发函数,并指定相关的调用参数. 此外,这两个方法还有另一个功能,就是她可以让一个对象去‘借用‘另一个对象的 ...

  10. Redis Sentinel 情况下bind地址设置

    Redis Sentinel 情况下bind地址设置 1个master,2个slave,3个sentinel的情况下,注意bind地址的时候不要写0.0.0.0,会导致绑定多个地址, 然后sentin ...