Unicode格式字符串之间转换
2016-12-27 13:22
351 查看
char*转CString
CString CharToUnicode(const char* str)
{
DWORD dwUnicodeLen; //转换后Unicode的长度
TCHAR *pwText; //保存Unicode的指针
CString strUnicode; //返回值
//获得转换后的长度,并分配内存
dwUnicodeLen = MultiByteToWideChar(CP_ACP,0,str,-1,NULL,0);
pwText = new TCHAR[dwUnicodeLen+1];
if (!pwText)
{
return strUnicode;
}
//转为Unicode
MultiByteToWideChar(CP_ACP,0,str,-1,pwText,dwUnicodeLen);
pwText[dwUnicodeLen]='\0';
strUnicode.Append(pwText);
delete [] pwText;
pwText = NULL;
return strUnicode;
}
char*转CString
char* UnicodeToChar(CString& str)
{
int len = WideCharToMultiByte(CP_ACP,0,str,-1,NULL,0,NULL,NULL);
char *pstr = new char[len +1];
if( NULL == pstr )
return NULL;
WideCharToMultiByte(CP_ACP,0,str,-1,pstr,len,NULL,NULL );
return pstr;
}
避免内存泄漏返回的字符串使用完之后记得要delete掉,如delete[] pstr;pstr=NULL;
//将wstring转换成string
std::string wstring2string(std::wstring wstr,char* chDevId)
{
std::string result;
//获取缓冲区大小,并申请空间,缓冲区大小事按字节计算的
int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
char* buffer = new char[len + 1];
//宽字节编码转换成多字节编码
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
buffer[len] = '\0';
strcpy_s(chDevId,255,buffer);
//删除缓冲区并返回值
result.append(buffer);
delete[] buffer;
return result;
}
CString CharToUnicode(const char* str)
{
DWORD dwUnicodeLen; //转换后Unicode的长度
TCHAR *pwText; //保存Unicode的指针
CString strUnicode; //返回值
//获得转换后的长度,并分配内存
dwUnicodeLen = MultiByteToWideChar(CP_ACP,0,str,-1,NULL,0);
pwText = new TCHAR[dwUnicodeLen+1];
if (!pwText)
{
return strUnicode;
}
//转为Unicode
MultiByteToWideChar(CP_ACP,0,str,-1,pwText,dwUnicodeLen);
pwText[dwUnicodeLen]='\0';
strUnicode.Append(pwText);
delete [] pwText;
pwText = NULL;
return strUnicode;
}
char*转CString
char* UnicodeToChar(CString& str)
{
int len = WideCharToMultiByte(CP_ACP,0,str,-1,NULL,0,NULL,NULL);
char *pstr = new char[len +1];
if( NULL == pstr )
return NULL;
WideCharToMultiByte(CP_ACP,0,str,-1,pstr,len,NULL,NULL );
return pstr;
}
避免内存泄漏返回的字符串使用完之后记得要delete掉,如delete[] pstr;pstr=NULL;
//将wstring转换成string
std::string wstring2string(std::wstring wstr,char* chDevId)
{
std::string result;
//获取缓冲区大小,并申请空间,缓冲区大小事按字节计算的
int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
char* buffer = new char[len + 1];
//宽字节编码转换成多字节编码
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
buffer[len] = '\0';
strcpy_s(chDevId,255,buffer);
//删除缓冲区并返回值
result.append(buffer);
delete[] buffer;
return result;
}
相关文章推荐
- asii和unicode格式字符串之间的相互转换
- Python中unicode编码的字符串和其他格式的字符串之间进行转换
- 字符串和(Unicode)十六进制数之间的转换方法包含(C#和VB6)
- JavaScript中扩展字符串与Date对象之间的转换,支持大多数日期格式的字符串(包括中文)
- VC中Ansi、Unicode、UTF8字符串之间的转换和写入文本
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
- Ansi、Unicode、UTF8字符串之间的转换,wprintf
- Ansi、Unicode、UTF8等编码字符串之间的转换和写入文本文件(学习中……)
- Ansi、Unicode、UTF8等编码字符串之间的转换和写入文本文件(学习中……)
- C++中Ansi、Unicode、UTF8字符串之间的转换和写入文本文件[zz]
- VC中Ansi、Unicode、UTF8字符串之间的转换和写入文本
- utf,unicode,ascii字符串之间的相互转换
- ANSI,UNICODE, UTF-8 字符串之间的相互转换
- Ansi、Unicode、UTF8字符串之间的转换和写入文本文件
- JavaScript中扩展字符串与Date对象之间的转换,支持大多数日期格式的字符串(包括中文)
- javascript里字符串与Unicode之间的转换
- Unicode字符串与CString字符串之间的互相转换
- MFC的中文路径的解决问题Ansi、Unicode、UTF8字符串之间的转换
- 字符串和(Unicode)十六进制数之间的转换方法包含(C#和VB6)