Window下,C++ 操作 Mysql、Url、utf-8文件 编码问题(读取和写入)
Mysql
读取mysql表,中文乱码
在sql语句执行前执行mysql_query(&mysql, "SET NAMES GB2312");
mysql_query(&mysql, "SET NAMES GB2312");
写入中文到mysql表,中文乱码
在sql语句执行前执行mysql_query(&mysql, "SET NAMES UTF8");
mysql_query(&mysql, "SET NAMES UTF8");
URL
当 utf-8格式的url 数据转成字符串,用UrlDecode_UTF8
当 utf-8格式的string 字符串转成普通的std::string,用UTF8_To_string
反之使用string_To_UTF8
和UrlEncode_UTF8
以下还提供GBK格式url的编码解码
Url:
byte toHex(const byte& x)
{return x > 9 ? x - 10 + 'A' : x + '0';
}byte fromHex(const byte& x)
{return isdigit(x) ? x - '0' : x - 'A' + 10;
}// URL解码,解码为GBKstd::string URLDecode(std::string strOrg){std::string sOut;for (size_t ix = 0; ix < strOrg.size(); ix++){byte ch = 0;if (strOrg[ix] == '%'){ch = (fromHex(strOrg[ix + 1]) << 4);ch |= fromHex(strOrg[ix + 2]);ix += 2;}else if (strOrg[ix] == '+'){ch = ' ';}else{ch = strOrg[ix];}sOut += (char)ch;}return sOut;}// URL编码,编码为GBKstd::string URLEncode(std::string strOrg){std::string sOut;for (size_t ix = 0; ix < strOrg.size(); ix++){byte buf[4];memset(buf, 0, 4);if (isalnum((byte)strOrg[ix])){buf[0] = strOrg[ix];}//else if ( isspace( (byte)strOrg[ix] ) ) //貌似把空格编码成%20或者+都可以 //{ // buf[0] = '+'; //} else{buf[0] = '%';buf[1] = toHex((byte)strOrg[ix] >> 4);buf[2] = toHex((byte)strOrg[ix] % 16);}sOut += (char*)buf;}return sOut;}std::string GBKToUTF8(const std::string strGBK){std::string strOutUTF8 = "";int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);wchar_t* str1 = new wchar_t[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;delete[] str2;return strOutUTF8;}std::string UTF8ToGBK(const std::string strUTF8){int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);wchar_t* wszGBK = new wchar_t[len + 1];memset(wszGBK, 0, (len + 1) * sizeof(WCHAR));MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)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;}// URL编码,编码为GBKstd::string UrlEncode_GBK(std::string strOrg){return URLEncode(strOrg);}// URL编码,编码为UTF-8std::string UrlEncode_UTF8(std::string strOrg){return URLEncode(GBKToUTF8(strOrg));}// URL解码,解码为GBKstd::string UrlDecode_GBK(std::string strOrg){return URLDecode(strOrg);}// URL解码,解码为UTF-8std::string UrlDecode_UTF8(std::string strOrg){return UTF8ToGBK(URLDecode(strOrg));}
std::string 和 utf-8 string 相互转换:
std::string string_To_UTF8(const std::string& str){int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);wchar_t* pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴ZeroMemory(pwBuf, nwLen * 2 + 2);::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);char* pBuf = new char[nLen + 1];ZeroMemory(pBuf, nLen + 1);::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);std::string retStr(pBuf);delete[]pwBuf;delete[]pBuf;pwBuf = NULL;pBuf = NULL;return retStr;}std::string UTF8_To_string(const std::string& str){int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);wchar_t* pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴memset(pwBuf, 0, nwLen * 2 + 2);MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);char* pBuf = new char[nLen + 1];memset(pBuf, 0, nLen + 1);WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);std::string retStr = pBuf;delete[]pBuf;delete[]pwBuf;pBuf = NULL;pwBuf = NULL;return retStr;}
std::fstream
当fstream对象读取utf-8文件,先将文件内容从Utf-8转为Unicode,再写到std::wstring中,传到fstream内。
示例:
#include <fstream>// 读取Utf-8格式的文件,准备写入std::fstream ifile("path", std::ios::in | std::ios::out);std::string buffer;std::wstring wstr = L"";if (!ifile.is_open()){std::cout << "文件打开失败!" << std::endl;return -1;}while (std::getline(ifile, buffer)){wstr += tool.UTF8ToUnicode(buffer) + L'\n';}ifile.close();// wstring写入utf-8格式文件std::fstream ofile("path", std::ios::out);std::locale oldLocale1 = ofile.imbue(std::locale(std::locale("chs"), "", LC_CTYPE));if (!ofile.is_open()){std::cout << "文件打开失败!" << std::endl;return -1;}ofile << tool.ToUTF8(wstr );ofile.close();ofile.imbue(oldLocale1);
// 转码函数// 1std::string UnicodeToUTF8(const std::wstring& wstr){std::string ret;try {std::wstring_convert< std::codecvt_utf8<wchar_t> > wcv;ret = wcv.to_bytes(wstr);}catch (const std::exception& e) {std::string str = e.what();}return ret;}std::wstring UTF8ToUnicode(const std::string& str){std::wstring ret;try {std::wstring_convert< std::codecvt_utf8<wchar_t> > wcv;ret = wcv.from_bytes(str);}catch (const std::exception& e) {std::string str = e.what();}return ret;}std::string ToUTF8(const wchar_t* buffer, int len){int size = ::WideCharToMultiByte(CP_UTF8, 0, buffer, len, NULL, 0, NULL, NULL);if (size == 0)return "";std::string newbuffer;newbuffer.resize(size);::WideCharToMultiByte(CP_UTF8, 0, buffer, len,const_cast<char*>(newbuffer.c_str()), size, NULL, NULL);return newbuffer;}// 2std::string ToUTF8(const std::wstring& str){return ToUTF8(str.c_str(), (int)str.size());}
Window下,C++ 操作 Mysql、Url、utf-8文件 编码问题(读取和写入)相关推荐
- mac php 连接mysql数据库_Mac环境下php操作mysql数据库的方法分享
Mac环境下php操作mysql数据库的方法分享 今天在mac上搭建好了php的环境,我们就把php操作mysql数据库的方法分享给大家,有需要的小伙伴参考下. Mac本地环境搭建 在Mac系统,我们 ...
- mac php 连接mysql数据库_Mac环境下php操作mysql数据库的方法分享_PHP教程
Mac环境下php操作mysql数据库的方法分享 今天在mac上搭建好了php的环境,我们就把php操作mysql数据库的方法分享给大家,有需要的小伙伴参考下. Mac本地环境搭建 在Mac系统,我们 ...
- linux c mysql教程_linux下c操作mysql之增删改查
书接上文,继续进行linux 下c操作mysql. 1.创建表/插入数据 mysql> desc children -> ; +---------+-------------+------ ...
- mysql5.6.24安装perl,linux下perl操作MySQL数据库(需要安装DBI)
这篇文章主要为大家详细介绍了linux下perl操作MySQL数据库(需要安装DBI),具有一定的参考价值,可以用来参考一下. 感兴趣的小伙伴,下面一起跟随512笔记的小编小韵来看看吧!DBI安装:D ...
- Win7下Python操作MySQL步骤
1.检视Python版本 如果尚未安装Python,那么你可以到Python官网进行下载: For the MD5 checksums and OpenPGP signatures, look ...
- mac下php mysql数据库文件怎么打开_Mac环境下php操作mysql数据库的方法分享
今天在mac上搭建好了php的环境,我们就把php操作mysql数据库的方法分享给大家,有需要的小伙伴参考下. Mac本地环境搭建 在Mac系统,我们可以使用MAMP Pro 软件来搭建本地服务器.安 ...
- c 文件怎么进行读取和写入操作?
C >>和<<读写文本文件: fstream 或者 ifstream 类负责实现对文件的读取,它们内部都对 >> 输出流运算符做了重载:同样,fstream 和 o ...
- 使用C#操作Oracle Spatial的SDO_GEOMETRY对像(读取和写入)
首先,这个需要使用ODAC,也就是Oracle.DataAccess.dll,新出的托管Oracle.ManagedDataAccess.dll不支持Object Type,无法使用 ODAC下载地址 ...
- Linux下首次操作MySQL失败
根据资料,某些版本Linux自带了MySQL:来看一下, 打个命令看一下:输出了一些内容:Linux不熟悉,看上去安装了一个低版本的mysql: 看一下有没有mysqld进程:ps aux ,查看进程 ...
最新文章
- HDU_Virtual Friends (并查集)
- rfid3-micro2440,linux2.6.32.2,写成misc驱动
- 阿里内部资料!如何试出一个Android开发者真正的水平?系列教学
- jQuery:无限循环两个或者多个事件 click / toggle between two functions
- (七)linux操作系统-linux韩顺平2021笔记
- 免费下载3小时学会Excel数据处理视频教程
- Java的责任链模式
- 信度spss怎么做_Spss详细图文教程——问卷信度和效度检验步骤图解
- ES集群状态检查报错:master_not_discovered_exception 503错误
- python京东笔试题象棋马走到指定位置方法数
- 四元数旋转表达(Hamilton notation JPL notation)
- ALSong-带有高级音效的漂亮音乐播放器(类似千千静听)
- 塑胶卡扣弹性计算公式_塑胶产品结构设计 卡扣
- elasticsearch 实现查询忽略大小写
- matlab毕业设计工作日志通用,毕业论文日志100篇通用_毕业论文日志100篇_万能工作日志100篇...
- HTML+CSS第十课:常见的3种网页布局方式:表格布局、DIV+CSS布局、框架布局
- 使用VScode简易编程
- 日常安全运营工作的一些思考
- Kerberos介绍(全)
- aes加密算法python语言实现_C#, Java, PHP, Python和Javascript几种语言的AES加密解密实现[转载]...