调用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 ...
随机推荐
- docker mysql 导入导出数据
导出数据 1.导出mysql单张表结构和数据: docker exec -it my-mysql mysqldump dbname -uroot -p123456 --tables tname > ...
- js-压缩混淆工具 uglifyjs
单个打包文件npm install uglify-js -g 使用uglifyjs压缩js uglifyjs 原始js文件 -m -c -o 压缩后js文件 uglifyjs 原始js文件 -b -c ...
- MyBatis_SelectKey使用oracle 序列插入主键
mapper 如下: 使用<selectkey>实现 也可以使用oracle的row 级触发器trigger实现: <?xml version="1.0" enc ...
- 从jar中读取资源文件的问题
在项目中遇到了一个问题,在IDE中读取配置文件的程序,打成架包以后,放到服务器上跑,报找不到资源文件的错误. 其中,资源文件的路径如下: 获取配置文件路径和读取的方法如下: private stati ...
- 管理SQL Server监控
http://blog.csdn.net/DBA_Huangzj/article/category/1133081 http://www.cnblogs.com/bhtfg538/archive/20 ...
- WebLogic 11g重置用户密码
weblogic安装后,很久不用,忘记访问控制台的用户名或者密码,可通过以下步骤来重置用户名密码. 版本:WebLogic Server 11g 说明:%DOMAIN_HOME%:指WebLogic ...
- Android 自定义录音、播放动画View,让你的录音浪起来
最近公司项目有一个录音的录制和播放动画需求,然后时间是那么紧,那么赶紧开撸. 先看效果图 嗯,然后大致就是这样,按住录音,然后有一个倒计时,最外层一个进度条,还有一个类似模拟声波的动画效果(其实中间的 ...
- android清除缓存为什么总是存在12k?
转载请注明出处:http://blog.csdn.net/droyon/article/details/41116529 android手机在4.2之后.清除缓存总是会残留12k的大小.预计强迫症患者 ...
- mac 下 virtualbox 配置全网通
mac下virtualbox实现主机和虚拟机.虚拟机和外网互访的方案 全局添加Host-Only网络 Adapter IPv4 Address:192.168.56.1 IPv4 Network Ma ...
- python 使用cx-freeze打包程序
python环境 3.6.5 win7 linux环境同理 先尝试了PyInstaller ,打包时一直提示 no module named gtk 而gtk 又依赖pygobj ...