直接上代码:
#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);