直接上代码:
#ifndef STRUTIL_H
#define STRUTIL_H
#include<string>
#include <strstream>
#include<xstring>
class StrUtil {
public:
static std::string UnicodeToUTF8(const std::wstring& wstr);
static std::wstring UTF8ToUnicode(const std::string& str);
static std::string UnicodeToANSI(const std::wstring& wstr);
static std::wstring ANSIToUnicode(const std::string& str);
static std::string UTF8ToANSI(const std::string& str);
static std::string ANSIToUTF8(const std::string& str);
static std::string toString(int val);
static std::string toString(float val);
static std::string toString(double val);
static int toInt(std::string val);
static float toFloat(std::string val);
static double toDouble(std::string val);
static double convertToDouble(std::string val);
};
#endif
cpp文件:
#include "StrUtil.h"
#include <locale>
#include <codecvt>
#include <iostream>
#include <wchar.h>
#include <windows.h>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
std::string StrUtil::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::cerr << e.what() << std::endl;
}
return ret;
}
std::wstring StrUtil::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::cerr << e.what() << std::endl;
}
return ret;
}
std::string StrUtil::UnicodeToANSI(const std::wstring& wstr)
{
//从宽字符串转换窄字符串
const wchar_t* sBuf = wstr.data();
//获取转换所需的目标缓存大小
DWORD dBufSize = WideCharToMultiByte(CP_ACP, 0, sBuf, -1, NULL, 0, NULL, FALSE);
//printf("需要char %u 个\n", dBufSize);
//分配目标缓存
char* dBuf = new char[dBufSize + 1];
memset(dBuf, 0, dBufSize + 1);
//转换
int nRet = WideCharToMultiByte(CP_ACP, 0, sBuf, -1, dBuf, dBufSize, NULL, FALSE);
if (nRet <= 0)
{
printf("转换失败\n");
}
std::string ret = dBuf;
delete[]dBuf;
return ret;
}
std::wstring StrUtil::ANSIToUnicode(const std::string& str)
{
const char* sBuf = str.data();
//获取输入缓存大小
int sBufSize = strlen(sBuf);
//获取输出缓存大小
//VC++ 默认使用ANSI,故取第一个参数为CP_ACP
DWORD dBufSize = MultiByteToWideChar(CP_ACP, 0, sBuf, sBufSize, NULL, 0);
//printf("需要wchar_t %u 个\n", dBufSize);
wchar_t* dBuf = new wchar_t[dBufSize + 1];
wmemset(dBuf, 0, dBufSize + 1);
//进行转换
int nRet = MultiByteToWideChar(CP_ACP, 0, sBuf, sBufSize, dBuf, dBufSize);
if (nRet <= 0)
{
printf("转换失败\n");
}
std::wstring ret = dBuf;
delete[]dBuf;
return ret;
}
std::string StrUtil::UTF8ToANSI(const std::string& str)
{
return UnicodeToANSI(UTF8ToUnicode(str));
}
std::string StrUtil::ANSIToUTF8(const std::string& str)
{
return UnicodeToUTF8(ANSIToUnicode(str));
}
string StrUtil::toString(int val)
{
return std::to_string(val);
}
string StrUtil::toString(float val)
{
return std::to_string(val);
}
string StrUtil::toString(double val)
{
return std::to_string(val);
}
int StrUtil::toInt(string val)
{
return std::stoi(val);
}
float StrUtil::toFloat(string val)
{
return std::stof(val);
}
double StrUtil::toDouble(string val)
{
return std::stod(val);
}
double StrUtil::convertToDouble(string val)
{
double d;
std::strstream ss;
ss << val;
ss >> d;
return d;
}
测试代码:
#include "StrUtil.h"
#include <iostream>
#include <stdio.h>
#include <tchar.h>
using namespace std;
int main()
{
wcout.imbue(locale("chs"));//将wcout的本地化语言设置为中文
cout.imbue(locale("chs"));
string ansiStr = "中国人";
wstring unicodeStr;
string utf8Str;
cout << "ansiStr = " << ansiStr << endl;
unicodeStr = StrUtil::ANSIToUnicode(ansiStr);
wcout << "unicodeStr = " << unicodeStr << endl;
//unicodeStr = L"美国人";
wcout <<"unicodeStr = "<< unicodeStr << endl;
ansiStr = StrUtil::UnicodeToANSI(unicodeStr);
cout << "ansiStr = " << ansiStr << endl;
utf8Str = StrUtil::UnicodeToUTF8(unicodeStr);
cout << "utf8Str = " << utf8Str << endl;
unicodeStr = StrUtil::UTF8ToUnicode(utf8Str);
wcout << "unicodeStr = " << unicodeStr << endl;
ansiStr = StrUtil::UnicodeToANSI(unicodeStr);
cout << "ansiStr = " << ansiStr << endl;
utf8Str = StrUtil::ANSIToUTF8(ansiStr);
cout << "utf8Str = " << utf8Str << endl;
ansiStr = StrUtil::UTF8ToANSI(utf8Str);
cout << "ansiStr = " << ansiStr << endl;
int i = 123;
string val = "";
val = StrUtil::toString(i);
cout << "val = " << val << endl;
float f = 20.32;
val = StrUtil::toString(f);
cout << "val = " << val << endl;
double d = 123456.987456;
val = StrUtil::toString(d);
cout << "val = " << val << endl;
string str = "100";
i = StrUtil::toInt(str);
cout << "i = " << i << endl;
str = "1122.2233";
f = StrUtil::toFloat(str);
cout << "f = " << f << endl;
str = "12345.123";
d = StrUtil::toDouble(str);
cout << "d = " << d << endl;
string::size_type sz;
cout << stod("12345.12345",&sz) << endl;
cout << stold("0.123456789", &sz) << endl;
cout << StrUtil::convertToDouble("12345.12345") << endl;
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门使用技巧:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
本文介绍了C++中进行基本编码转换的方法,包括不同编码间的转换以及string类型与其他数据类型的互转。通过示例代码,详细展示了在Windows环境下C++如何操作字符串编码,帮助开发者更好地理解和应用C++的字符串处理。
380

被折叠的 条评论
为什么被折叠?



