以下为在VS环境Unicode编码模式下测试可行的相关类型转换
一、 void* 转 char* (同const char*,强制类型转换)
(1) void *pStr = "abcdef";
char *pCh = (char*)pStr;
(2) void *pStr = "abcdef";
//分配strlen((const char*)pStr)+1个字节的空间是为了完全存下pStr(最后一个字符\0)
char *pSz = new char[strlen((const char*)pStr)+1]; //等同const int n = strlen((const char*)pStr)+1; char pSz[n];
memset(pSz, 0x00, strlen((const char*)pStr)+1);
memcpy(pSz, pStr, strlen((const char*)pStr));
delete pSz;
二、 char*转unsigned int、int、long、float、double、char[]、string、CString
char *pStr = "48";
// unsigned int(#include )
unsigned int uNum = strtoul(pStr, NULL, 10); //strtoul(待转换的字符串,endptr,10进制);
// int(#include )
int nNum = atoi(pStr);
// long(#include )
long lNum = strtol(pStr, NULL, 10); //strtol(待转换的字符串,endptr,10进制);
// float(#include )
float fNum = atof(pStr);
// double(#include )
double dNum = strtod(pStr, NULL);
// char[](#include )
char szNum[256] = { 0 };
//strcpy_s(szNum, pStr); //非二进制数据
memcpy(szNum, pStr, strlen(pStr)); //二进制数据、非二进制数据均可
// string
string strNum(pStr); //构造函数
// CString
CString cstrNum(pStr);
三、
unsigned int、int、long、float、double、string、CString转char*
char *pStr = nullptr;
char szCh[25];
// unsigned int
unsigned int uNum = 48;
pStr = itoa(uNum, szCh, 10); //itoa(待转换数字,接收结果的字符串,10进制);
// int
int nNum = 48;
itoa(nNum, szCh, 10); //itoa(待转换数字,接收结果的字符串,10进制);
// long
long lNum = 48;
ltoa(lNum, szCh, 10); //ltoa(待转换数字,接收结果的字符串,10进制);
// float
float fNum = 48;
sprintf_s(szCh, "%f", fNum); //sprintf_s(接收结果的字符串,待转换数字格式,待转换数字)
// double
double dNum = 48;
sprintf_s(szCh, "%lf", dNum);
// string
string strNum = "abcdef"; //构造函数
strcpy_s(szCh, strNum.c_str());
// CString
USES_CONVERSION;
CString cstrNum = L"abcdef";
strcpy_s(szCh, T2A(cstrNum));
四、 unsigned int、int、long、unsigned long、double转string、CString
// unsigned int、int、long、unsigned long、double转string
int nNum = 48;
string strNum = to_string(nNum); //同unsigned int、long、unsigned long、double
// unsigned int、int、long、unsigned long、double转CString
int nNum = 48;
CString cstrNum;
cstrNum.Format(L"%d", nNum);//同unsigned int、long、unsigned long、double取格式不同
五、 string转CString
(1) string strStr = "abcdef";
CString strCStr(strStr.c_str());
(2) USES_CONVERSION;
string strStr = "abcdef";
CString strCStr;
strCStr = A2T(strStr.c_str());
六、 CString转string
(1) CString strCStr = L"abcdef";
string strStr = CStringA(strCStr);
(2) USES_CONVERSION;
CString strCStr = L"abcdef";
string strStr(T2A(strCStr));
(3) //LPCWSTRתstring
string WChar2Ansi(LPCWSTR pwszSrc)
{
int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
if (nLen <= 0)
return std::string("");
char* pszDst = new char[nLen];
if (NULL == pszDst)
return std::string("");
WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
pszDst[nLen - 1] = 0;
std::string strTemp(pszDst);
delete[] pszDst;
return strTemp;
}
七、 char*转CString(解决中文乱码)
char* pFileName = "abcdef";
//计算char *数组大小,以字节为单位,一个汉字占两个字节
int charLen = strlen(pFileName);
//计算多字节字符的大小,按字符计算。
int len = MultiByteToWideChar(CP_ACP, 0, pFileName, charLen, NULL, 0);
//为宽字节字符数组申请空间,数组大小为按字节计算的多字节字符大小
TCHAR *buf = new TCHAR[len + 1];
//多字节编码转换成宽字节编码
MultiByteToWideChar(CP_ACP, 0, pFileName, charLen, buf, len);
buf[len] = '\0'; //添加字符串结尾,注意不是len+1
//将TCHAR数组转换为CString
CString pWideChar;
pWideChar.Append(buf);
//删除缓冲区
delete[]buf;
八、 CString转char*(解决中文乱码)
CString str = L"abcdef";
//注意:以下n和len的值大小不同,n是按字符计算的,len是按字节计算的
int n = str.GetLength(); // n = 14, len = 18
//获取宽字节字符的大小,大小是按字节计算的
int len = WideCharToMultiByte(CP_ACP, 0, str, str.GetLength(), NULL, 0, NULL, NULL);
//为多字节字符数组申请空间,数组大小为按字节计算的宽字节字节大小
char * pFileName = new char[len + 1]; //以字节为单位
//宽字节编码转换成多字节编码
WideCharToMultiByte(CP_ACP, 0, str, str.GetLength(), pFileName, len, NULL, NULL);
pFileName[len + 1] = '\0'; //多字节字符以'\0'结束
九、 utf8转GB2312
BOOL U2G(const char* utf8, char *szOut, const int &nOutLen)
{
int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
if (nOutLen < len){
return FALSE;
}
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, (len + 1)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
memset(szOut, 0, nOutLen);
WideCharToMultiByte(CP_ACP, 0, wstr, -1, szOut, len, NULL, NULL);
if (NULL != wstr)
delete[] wstr;
return TRUE;
}
十、 GB2312转utf8
BOOL G2U(const char* gb2312, char *szOut, const int &nOutLen)
{
int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
if (nOutLen < len){
return FALSE;
}
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, (len + 1)*sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
memset(szOut, 0, nOutLen);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, szOut, len, NULL, NULL);
if (NULL != wstr)
delete[] wstr;
return TRUE;
}