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. How to debug Android Native Application with eclipse

    This blog is inspired by this tutorial http://mhandroid.wordpress.com/2011/01/23/using-eclipse-for-a ...

  2. 关于C++的new是否会对内存初始化的问题

    先把结论放上来: C++在new时的初始化的规律可能为:对于有构造函数的类,不论有没有括号,都用构造函数进行初始化:如果没有构造函数,则不加括号的new只分配内存空间,不进行内存的初始化,而加了括号的 ...

  3. Python日志(logging)模块使用方法简介

    A logger is configured to have a log level. This log level describes the severity of the messages th ...

  4. svn不是内部或外部命令?

    svn不是内部或外部命令? 我的系统是Win7, [计算机]-->右键[属性]-->[高级系统设置]-->[环境变量]-->[系统变量 (S)]-->[Path]--&g ...

  5. [ASP.NET Core] Tips

    让Cache支持SetObject using Microsoft.AspNetCore.Http; using Newtonsoft.Json; public static class Sessio ...

  6. python学习之-- 故障记录汇总

    以下为我编程期间遇到的错误并进行记录,起始时间2017-6-21 时间:2018/11/21问题现象:ajax 执行异步提交后,在访问日志看出现了2次post执行分析:默认ajax提交是执行一次,然后 ...

  7. .net 哈希

    using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptograph ...

  8. pt-pmp :pt toolkit

    http://www.cnblogs.com/ivictor/p/6012183.html

  9. VC++ ADO 连接 mysql

    通过自己摸索和网上帮助 了解了VC++ 用ADO 连接mysql数据库的方法:     使用的方法是利用ADO通过建立ODBC数据源来最终达到访问MySQL的目的.     1.安装mysql数据库服 ...

  10. js随机数 从头开始系列

    js要常常写啊要不然就要从0开始 1 var num = Math.random(); //创建一个0-1随机数字 num*=10 //变为0-10随机数字 //有好几种取整方式 var i = Ma ...