c++ 各个类型之间转化实例

c++ 中:

#pragma once

#include <string>
using namespace std;

class CUtil
{
public:
/*
*  int 转化为string
*/
static string IntToString(int iSour);


/*
* double 转化为string
*/
static string DoubleToString(double dValue);


/*
* char 转化为string
*/
static string CharToString(char cValue);


/*
* 十进制的数转化为16进制的字符串
*/
static void DToHexString(unsigned int n, string &strValue);


/*
* 从第nLen+1位开始截取之后的整个字符串
*/
static void GetSplitLTString(string &strSour, string &strDes,int nLen);


/*
* string转化为char*
*/
static char* StringToChar(string &strSour);


/*
* 去除字符串中的所有空格
*/
static void RemoveAllBlankSpace(string &strSour);


/*
* 判断文件是否存在
*/
static bool isExitFile(string& strPath);
};






#include "stdafx.h"
#include "CUtil.h"
#include <io.h>
#include "Windows.h"
#include <sstream>




string CUtil::IntToString(int iSour)
{
std::stringstream ss;
ss << iSour;
string s = ss.str();
ss.clear();
return s;
}




string CUtil::DoubleToString(double dValue)
{
std::stringstream ss;
ss << dValue;
string s = ss.str();
ss.clear();
return s;
}


string CUtil::CharToString(char cValue)
{
string str;
stringstream stream;
stream << cValue;
str = stream.str();
stream.clear();
return str;
}


char *CUtil::StringToChar(string &strSour)
{
char* c;
const int len = strSour.length();
c = new char[len + 1];
strcpy(c, strSour.c_str());
return c;
}


void CUtil::DToHexString(unsigned int n,string &strValue)
{
if (n == 0)
{
return;
}
DToHexString(n / 16, strValue);
int m = n % 16;
char cValue = char(m + (m < 10 ? '0' : 'A' - 10));
string str = CUtil::CharToString(cValue);
strValue = strValue + str;
}


void CUtil::GetSplitLTString(string &strSour, string &strDes, int nLen)
{
strDes = strSour.substr(nLen, strSour.length() - nLen);
int yy = 55;
}


void CUtil::RemoveAllBlankSpace(string &strSour)
{
int begin = 0;
begin = strSour.find(" ", begin);  //查找空格在字符串中第一次出现的位置
while (begin != -1)  //表示字符串中存在空格
{
strSour.replace(begin, 1, "");  // 用空串替换str中从begin开始的1个字符
begin = strSour.find(" ", begin);  //查找空格在替换后的str中第一次出现的位置
}
}


bool CUtil::isExitFile(string& strPath)
{
TCHAR buf[MAX_PATH];
if (-1 == _access(strPath.c_str(), 0))
{
return false; //文件不存在为
}
return true; //文件存在
}


MFC中:

#pragma once


class CStringUtil
{
public:


/*
*  int 转化为string
*/
static string IntToString(int iSour);


//double 转化为string
static string DoubleToString(double dValue);


/*
*  string 转化为 wstring
*/
static std::wstring StringToWString(const std::string &str);


/*
*  UTF8 转化为 Encode
*/
static char* UTF8ToEncode(const char* mbcsStr);


/*
*  Encode 转化为 UTF8
*/
static char* EncodeToUTF8(const char* mbcsStr);


static bool WStringToString(const wstring &wstr, string &str);


/*
*  char* 转化为 TCHAR*
*/
static TCHAR *CStringUtil::CHAR2TCHAR(char *ch);


static void ConvertUtf8ToGBK(std::string&amp, std::string strUtf8);
static void ConvertGBKToUtf8(std::string& amp, std::string strGBK);
};


#include "stdafx.h"
#include "CStringUtil.h"
#include <sstream>
using namespace std;

string CStringUtil::IntToString(int iSour)
{
std::stringstream ss;
ss << iSour;
string s = ss.str();
ss.clear();
return s;
}




string CStringUtil::DoubleToString(double dValue)
{
std::stringstream ss;
ss << dValue;
string s = ss.str();
ss.clear();
return s;
}


//wstring高字节不为0,返回FALSE
std::wstring CStringUtil::StringToWString(const std::string &str)
 {    
wstring wstr;
     int nLen = (int)str.length();    
     wstr.resize(nLen,L'');
     MultiByteToWideChar(CP_ACP,0,(LPCSTR)str.c_str(),nLen,(LPWSTR)wstr.c_str(),nLen);
return wstr;
 }


char* CStringUtil::EncodeToUTF8(const char* mbcsStr) 

wchar_t*  wideStr; 
char*   utf8Str; 
int   charLen;


charLen = MultiByteToWideChar(CP_UTF8, 0, mbcsStr, -1, NULL, 0); 
wideStr = (wchar_t*) malloc(sizeof(wchar_t)*charLen); 
MultiByteToWideChar(CP_ACP, 0, mbcsStr, -1, wideStr, charLen);


charLen = WideCharToMultiByte(CP_UTF8, 0, wideStr, -1, NULL, 0, NULL, NULL);


utf8Str = (char*) malloc(charLen);


WideCharToMultiByte(CP_UTF8, 0, wideStr, -1, utf8Str, charLen, NULL, NULL);


free(wideStr); 
return utf8Str;



char* CStringUtil::UTF8ToEncode(const char* mbcsStr)
{
wchar_t*  wideStr; 
char*   unicodeStr; 
int   charLen;


charLen = MultiByteToWideChar(CP_UTF8, 0, mbcsStr, -1, NULL, 0);   
wideStr = (wchar_t*) malloc(sizeof(wchar_t)*charLen); 
MultiByteToWideChar(CP_UTF8, 0, mbcsStr, -1, wideStr, charLen); 


charLen =WideCharToMultiByte(CP_ACP, 0, wideStr, -1, NULL, 0, NULL, NULL);  
unicodeStr = (char*)malloc(charLen);
WideCharToMultiByte(CP_ACP, 0, wideStr, -1, unicodeStr, charLen, NULL, NULL); 


free(wideStr); 
return unicodeStr;
}




bool CStringUtil::WStringToString(const wstring &wstr, string &str)
{    
int nLen = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wstr.c_str(), -1, NULL, 0, NULL, NULL);
if (nLen<= 0) return false;


char* pszDst = new char[nLen];
if (NULL == pszDst) return false;


WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wstr.c_str(), -1, pszDst, nLen, NULL, NULL);
pszDst[nLen -1] = 0;
str = pszDst;
delete [] pszDst;


return true; 
}


TCHAR *CStringUtil::CHAR2TCHAR(char *ch)
{
TCHAR Name[100];
#ifdef UNICODE
MultiByteToWideChar(CP_ACP, 0, ch, -1, Name, 100);
#else
strcpy(Name, ch);
#endif
return Name;
}


void CStringUtil::ConvertGBKToUtf8(std::string& amp, std::string strGBK)

int len=MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strGBK.c_str(), -1, NULL,0); 
unsigned short * wszUtf8 = new unsigned short[len+1]; 
memset(wszUtf8, 0, len * 2 + 2); 
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strGBK.c_str(), -1, (LPWSTR)wszUtf8, len); 
len = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)wszUtf8, -1, NULL, 0, NULL, NULL); 
char *szUtf8=new char[len + 1]; 
memset(szUtf8, 0, len + 1); 
WideCharToMultiByte (CP_UTF8, 0, (LPCWSTR)wszUtf8, -1, szUtf8, len, NULL,NULL); 
amp=szUtf8;
delete[] szUtf8; 
delete[] wszUtf8; 
}


void CStringUtil::ConvertUtf8ToGBK(std::string&amp, std::string strUtf8)

int len=MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUtf8.c_str(), -1, NULL,0); 
unsigned short * wszGBK = new unsigned short[len+1]; 
memset(wszGBK, 0, len * 2 + 2); 
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUtf8.c_str(), -1, (LPWSTR)wszGBK, len); 
len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wszGBK, -1, NULL, 0, NULL, NULL); 
char *szGBK=new char[len + 1]; 
memset(szGBK, 0, len + 1); 
WideCharToMultiByte (CP_ACP, 0, (LPCWSTR)wszGBK, -1, szGBK, len, NULL,NULL); 
//strUtf8 = szGBK; 
amp=szGBK;
delete[] szGBK; 
delete[] wszGBK; 
}

以上不完整地方以后再补充,时间匆匆,若有重复之处,莫吐槽。。。。。。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值