用c++写安卓程序,怎么进行编码转换,GBK 和 UTF-8 互相转换,谁能提供一个例子我用的开发工具是 Android Studio3.1.2 ,有人推荐了 iconv,可是从来没成功过。 请高手赐教
解决方案 »
- android 模拟器异常 the application launcher has stopped unexpected,该怎么解决呢?急求
- android edittext样式问题
- 很妖的事,关于android内edittext 求高手指点
- 希望大侠推荐Android开发教程
- 如何让一个service监听按键事件
- 中文标点符号的问题
- 各位大牛,Camera模块预览界面size的问题
- 小妹请教如何在下载的同时往Notification添加一个进度条。
- Android开发 客户端的通信
- Android Studio 2.2.3 创建空白模板编译出错
- android如何得到用户的运动方向?
- RecyclerView+ViewPager的实现问题
#include <string>
#include <fstream>
#include <windows.h> using namespace std;string GBKToUTF8(const std::string& strGBK)
{
string strOutUTF8 = "";
WCHAR * str1;
int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
str1 = new WCHAR[n];
MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);
n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
char * str2 = new char[n];
WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
strOutUTF8 = str2;
delete[]str1;
str1 = NULL;
delete[]str2;
str2 = NULL;
return strOutUTF8;
}string UTF8ToGBK(const std::string& strUTF8)
{
int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);
unsigned short * wszGBK = new unsigned short[len + 1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUTF8.c_str(), -1, wszGBK, len); len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK = new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte(CP_ACP,0, wszGBK, -1, szGBK, len, NULL, NULL);
//strUTF8 = szGBK;
std::string strTemp(szGBK);
delete[]szGBK;
delete[]wszGBK;
return strTemp;
}int _tmain(int argc, _TCHAR* argv[])
{
string test("我们中国是个强大的名族,强大的动力来自每个人的支持");
fstream output("test.txt",ios_base::out | ios_base::app);
output << GBKToUTF8(test);
//system("iconv -f GBK -t utf-8");
return 0;
}