C++基本的编码转换以及string类型互转

本文介绍了C++中进行基本编码转换的方法,包括不同编码间的转换以及string类型与其他数据类型的互转。通过示例代码,详细展示了在Windows环境下C++如何操作字符串编码,帮助开发者更好地理解和应用C++的字符串处理。

直接上代码:

#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 文件

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程经验随笔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值