字符转换的一些帖子,看到了就收藏一下,不错~
来源:互联网 发布:路由器端口是什么 编辑:程序博客网 时间:2024/05/12 16:15
char*、TCHAR*转换CString
CString str(****)
下面详细写一下其它转换
//////////////////////////////
/*
***********************************************************************
* 函数: TransCStringToTCHAR
* 描述:将CString 转换为 TCHAR*
* 日期:
***********************************************************************
*/
TCHAR* CPublic::CString2TCHAR(CString &str)
{
int iLen = str.GetLength();
TCHAR* szRs = new TCHAR[iLen];
lstrcpy(szRs, str.GetBuffer(iLen));
str.ReleaseBuffer();
return szRs;
}
/*
***********************************************************************
* 函数: TCHAR2Char
* 描述:将TCHAR* 转换为 char*
* 日期:
***********************************************************************
*/
char* TCHAR2char(TCHAR* tchStr)
{
int iLen = 2*wcslen(tchStr);//CString,TCHAR汉字算一个字符,因此不用普通计算长度
char* chRtn = new char[iLen+1]
wcstombs(chRtn,tchStr,iLen+1);//转换成功返回为非负值
return chRtn;
}
/*
***********************************************************************
* 函数: char2tchar
* 描述:将 char* 转换为 TCHAR*
* 日期:
***********************************************************************
*/
TCHAR *char2tchar(char *str)
{
int iLen = strlen(str);
TCHAR *chRtn = new TCHAR[iLen+1];
mbstowcs(chRtn, str, iLen+1);
return chRtn;
}
/*
***********************************************************************
* 函数: CString2char
* 描述:将CString转换为 char*
* 日期:
***********************************************************************
*/
char* CPublic::CString2char(CString &str)
{
int len = str.GetLength();
char* chRtn = (char*)malloc((len*2+1)*sizeof(char));//CString的长度中汉字算一个长度
memset(chRtn, 0, 2*len+1);
USES_CONVERSION;
strcpy((LPSTR)chRtn,OLE2A(str.LockBuffer()));
return chRtn;
}
//参考
///////////////////////
//Pocket PC上的UNICODE和ANSI字符串
//By Vassili Philippov, September 26, 2001.
//杨方思歧 译
////////////////////////
/*
***********************************************************************
* 函 数 名:GetAnsiString
* 描 述:将CString(unicode)转换为char*(ANSI)
* 参 数:CString &s 要转换的CString
* 返 回 值:返回转换结果
* 创建日期:
* 最后修改:
***********************************************************************
*/
char* GetAnsiString(const CString &s)
{
int nSize = 2*s.GetLength();
char *pAnsiString = new char[nSize+1];
wcstombs(pAnsiString, s, nSize+1);
return pAnsiString;
}
//////////////////////////////////////////////////////////////////////////////////////////////
WideCharToMultiByte和MultiByteToWideChar函数的用法
支持Unicode编码,需要多字节与宽字节之间的相互转换
WideCharToMultiByte的代码页用来标记与新转换的字符串相关的代码页。
MultiByteToWideChar的代码页用来标记与一个多字节字符串相关的代码页。
常用的代码页由CP_ACP和CP_UTF8两个。
使用CP_ACP代码页就实现了ANSI与Unicode之间的转换。
使用CP_UTF8代码页就实现了UTF-8与Unicode之间的转换。
wstring AnsiToUnicode(( const string& str )
{
int len = 0;
len = str.length();
int unicodeLen = ::MultiByteToWideChar( CP_ACP, 0, str.c_str(),-1,NULL,0 );
wchar_t * pUnicode;
pUnicode = new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
::MultiByteToWideChar( CP_ACP,0, str.c_str(),-1, (LPWSTR)pUnicode, unicodeLen );
wstring rt;
rt = ( wchar_t* )pUnicode;
delete pUnicode;
return rt;
}
string UnicodeToAnsi( const wstring& str )
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_ACP, 0, str.c_str(), -1, NULL, 0, NULL, NULL );
pElementText = new char[iTextLen + 1];
memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
::WideCharToMultiByte( CP_ACP, 0, str.c_str(), -1, pElementText,iTextLen,NULL,NULL );
string strText;
strText = pElementText;
delete[] pElementText;
return strText;
}
wstring UTF8ToUnicode(( const string& str )
{
int len = 0;
len = str.length();
int unicodeLen = ::MultiByteToWideChar( CP_UTF8, 0, str.c_str(),-1,NULL,0 );
wchar_t * pUnicode;
pUnicode = new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
::MultiByteToWideChar( CP_UTF8,0, str.c_str(),-1, (LPWSTR)pUnicode, unicodeLen );
wstring rt;
rt = ( wchar_t* )pUnicode;
delete pUnicode;
return rt;
}
string UnicodeToUTF8( const wstring& str )
{
char* pElementText;
int iTextLen;
// wide char to multi char
iTextLen = WideCharToMultiByte( CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL );
pElementText = new char[iTextLen + 1];
memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
::WideCharToMultiByte( CP_UTF8, 0, str.c_str(), -1, pElementText,iTextLen,NULL,NULL );
string strText;
strText = pElementText;
delete[] pElementText;
return strText;
}
- 字符转换的一些帖子,看到了就收藏一下,不错~
- 朋友收藏的一些站点,自己觉得还不错就拿过来用了。
- 今天看到了'\xff'这样的字符,记录一下
- 看这个帖子写的不错,就转来收藏了..(转asp.net为静态页)
- 浏览帖子看到的一些ASCII贴图
- 今天看到了一款mini小画板的程序,看起来不错,跟大家分享一下
- 网上看到的收藏了
- 做一个程序员需要哪些基本知识----网上看到的,觉得不错,就复制下来了
- 解决乱码问题 帖子看到的 留存一下
- 一个很好的模拟弹出窗口的特效 偶尔看到了收藏一下- -b
- struct详解(看到不错就转过来了)
- 今天看到一篇文章,收藏了很多大牛的博客,在这里分享一下
- 不错的Android开发资料,收藏一下
- 看到一些百度网盘上不错的资源
- 朋友发了一篇关于接口跟抽象类的文章,觉得不错收藏一下
- 看到的帖子
- 偶然看到一个不错的心理学网站,推荐一下
- 不错的C#代码库,偶然看到,分享一下
- Scala学习笔记6 (io)
- SQL server2000自带的数据库
- 李宁救赎:药不对症
- Struts2+DAO下的开发流程
- URI和URL的区别
- 字符转换的一些帖子,看到了就收藏一下,不错~
- 好博客
- 数据库读写分离
- 数据库连接池C3P0学习
- java基本数据类型
- linux页面缓存【笔记】
- JAVA游戏开发计算显示FPS
- VHDL数据类型
- xcode 5 cocos2d-x3.0 一 环境搭建