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. elementary os 0.4.1下编译GCC-7.1源码并安装成功

    参考文章:http://www.2cto.com/os/201402/281131.html 前几天为了图个新鲜,安装了elementary os 0.4.1,的确是一股清流,可惜的是gcc版本觉得有 ...

  2. LeetCode OJ——Convert Sorted Array to Binary Search Tree

    http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 将一个升序的数组转换成 height balan ...

  3. 解决npm 的 shasum check failed for错误

    使用npm安装一些包失败,类似如下报错情况:   C:\Program Files\nodejs>npm update npm npm ERR! Windows_NT 10.0.14393 np ...

  4. asp传递参数的几种方式

    把下列代码分别加入a.asp和b.asp的<body></body>中,点提交,就可以将a.asp文本框的内容传给b.asp并显示出来 a.ASP <form actio ...

  5. python常用模块2

    collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdict. ...

  6. L1-2. 点赞【求多组数据中出现次数最多的】

    L1-2. 点赞 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 微博上有个“点赞”功能,你可以为你喜欢的博文点个赞表示支持.每 ...

  7. Codeforces 235 E Number Challenge

    Discription Let's denote d(n) as the number of divisors of a positive integer n. You are given three ...

  8. openlayer3 加载geoserver发布的WFS服务

    转自原文 openlayer3加载geoserver发布的WFS服务 openlayers3调用GeoServer发布的wfs 1 参考一 1.1 问题 openlayer3加载WFS存在跨域问题,需 ...

  9. ubuntu navicat for mysql破解

    ubuntu navicat for mysql破解 ubuntu navicat for mysql只能试用14天. 破解方法:rm -rf /home/cxg/.navicat64/

  10. 【spring data jpa】启动报错:nested exception is java.util.NoSuchElementException

    spring boot项目中 使用spring data jpa 启动报错: org.springframework.beans.factory.UnsatisfiedDependencyExcept ...