调用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岁。\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的相互转换的更多相关文章
- C#调用windows API的一些方法
使用C#调用windows API(从其它地方总结来的,以备查询) C#调用windows API也可以叫做C#如何直接调用非托管代码,通常有2种方法: 1. 直接调用从 DLL 导出的函数. 2. ...
- C#调用Windows API函数截图
界面如下: 下面放了一个PictureBox 首先是声明函数: //这里是调用 Windows API函数来进行截图 //首先导入库文件 [System.Runtime.InteropServices ...
- 【转】用C#调用Windows API向指定窗口发送
一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.InteropServices; 2.引用需要使用的方法,格式 ...
- C#中调用Windows API的要点 .
介绍 API(Application Programming Interface),我想大家不会陌生,它是我们Windows编程的常客,虽然基于.Net平台的C#有了强大的类库,但是,我们还是不能否认 ...
- c# 判断窗体是否永在最前(TopMost),调用windows API
许多程序都可以把自身的窗体设为最前显示状态,这个可以参考博客c#让窗体永在最前 调用windows api 将窗体设为topmost.那么如何判断桌面上的一个窗体是否为最前显示状态呢,不光是自己的程序 ...
- c#让窗体永在最前 调用windows api 将窗体设为topmost
有时候应用程序需要将一个窗体始终位于屏幕的最前面,即使切换到其它窗体也能看到该窗体,这样的窗体就叫做TopMost窗体. 用C#制作TopMost窗体之前,首先要了解如何声明SetWindowPos函 ...
- 善于 调用Windows API
前一段时间看见别人做的一个自动填写信息并且点击登录的程序,觉得很有意思. 其实就是在程序中调用Windows的API,那么如何调用,下面就做个简单的介绍. 写的简单粗暴, 不喜轻喷. 0.首先引入名称 ...
- C#中调用Windows API时的数据类型对应关系
原文 C#中调用Windows API时的数据类型对应关系 BOOL=System.Int32 BOOLEAN=System.Int32 BYTE=System.UInt16 CHAR=System. ...
- 用C#调用Windows API向指定窗口发送按键消息 z
用C#调用Windows API向指定窗口发送 一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.Interop ...
随机推荐
- strace工具的实现原理【转】
转自:http://blog.csdn.net/jasonchen_gbd/article/details/44044539 版权声明:本文为博主原创文章,转载请附上原博链接. 目录(?)[-] ...
- rp6818 和rp4418 重点修改的kernel几个问题!
kernel\arch\arm\plat-s5p6818\drone\include文件下#define PAD_GPIOD1 (PAD_MODE_ALT | PAD_FUNC_ALT1 | ...
- Django的日志中关闭elasticsearch模块的日志
今天用python的日志模块,为Django项目配置了日志,运行的时候发现日志在疯狂的涨,检查后发现竟然是elasticsearch的日志,但是我没有打这个日志啊,根据日志提供的文件位置,我在elas ...
- vue生命周期回调方法
最近在用vue开发一个商品列表页,因需要根据请求回的字段是否有内容来显示隐藏该字段, 但因为vue异步加载导致显示隐藏方法不起作业(主要是判断条件取不到页面渲染内容),围观了vue生命周期后发现upd ...
- 查找系列合集-二叉查找树BST
一. 二叉树 1. 什么是二叉树? 在计算机科学中,二叉树是每个结点最多有两个子树的树结构. 通常子树被称作“左子树”(left subtree)和“右子树”(right subtree). 二叉树常 ...
- CODECHEF Oct. Challenge 2014 Children Trips
@(XSY)[分塊, 倍增] Description There's a new trend among Bytelandian schools. The "Byteland Tourist ...
- Neural Networks for Machine Learning by Geoffrey Hinton (1~2)
机器学习能良好解决的问题 识别模式 识别异常 预測 大脑工作模式 人类有个神经元,每一个包括个权重,带宽要远好于工作站. 神经元的不同类型 Linear (线性)神经元 Binary thresho ...
- Oracle数据库有用函数
有用函数 DECODE 语法例如以下: DECODE(value, if1, then1, if2,then2,if3,then3, . . . else ) Value 代表某个表的不论什么类型的 ...
- react 路由传参
今天,我们要讨论的是react router中Link传值的三种表现形式.分别为通过通配符传参.query传参和state传参. ps:进入正题前,先说明一下,以下的所有内容都是在react-rout ...
- 赵雅智_Android案例_刮刮乐
实现效果 主要代码 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...