String,CString,TCHAR*,char*之间区别和联系

TCHAR是一种字符串类型,它让你在以MBCS和UNNICODE来build程序时可以使用同样的代码,不需要使用繁琐的宏定义来包含你的代码,而char代表ASCII的字符

#ifdef UNICODE   
typedef wchar_t TCHAR;   
#else   
typedef char TCHAR;  
#endif

所以用MBCS来build时,TCHAR是char,使用UNICODE时,TCHAR是wchar_t.至于String,CString他们都是一种封装了字串处理的操作!你可以打开他们的源代码找寻究竟! CString属于VC的类库string是标准C++的类库string.h是C的库函数。

研究它们的源代好些!

TCHAR,char是可以同整型互换的类型。String,CString代表了一块内存区域。

TCHAR 赋值

#include <tchar.h>

TCHAR szWindowText[256]=_T("QQ2006");

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;    
}   
/***********************************************************************   
* 函数: THCAR2Char   
* 描述:将TCHAR* 转换为 char*   
* 日期:  
***********************************************************************   
*/    
char* CPublic::THCAR2char(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;
}


以下是调试中遇到的问题:

 

/在TCHAR*中存储的中文字符转换为char*时遇到的问题,

TCHAR tszName[MAX_PATH]=TEXT("刘小猪");

我一直认为求长度, 直接用 nLen = _tcslen(tszName)*sizeof(TCHAR);就可以了

调试过程中发现, 转换过来的字符总是少一个汉字.不明白为什么.

如果利用

int nLen = WideCharToMultiByte(CP_ACP, 0, tszName, -1, NULL, 0, NULL, NULL);

求出实际转换成char时的字符长度.

再去分配实际需要的内存空间的话就没有问题了.

char* pszName = new char[nLen*sizeof(char)];

WideCharToMultiByte(CP_ACP, 0, tszName, -1, pszName, nLen, NULL, NULL);


MultiByteToWideChar的使用:


int nLen = MultiByteToWideChar(CP_ACP, 0, pMultiByteStr, -1, NULL, 0);

TCHAR* pWideChar = new TCHAR[nLen * sizeof(TCHAR)];

MultiByteToWideChar(CP_ACP, 0, pMultiByteStr, -1, pWideChar, nLen);

 


  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目录 1 正文 4 一、 C++string的使用 4 1.1 C++ string简介 4 1.2 string的成员 4 1.2.1 append 4 1.2.2 assign 5 1.2.3 at 5 1.2.4 begin 6 1.2.5 c_str 6 1.2.6 capacity 6 1.2.7 clear 7 1.2.8 compare 7 1.2.9 copy 7 1.2.10 _Copy_s 7 1.2.11 data 7 1.2.12 empty 7 1.2.13 end 7 1.2.14 erase 7 1.2.15 find 7 1.2.16 find_first_not_of 8 1.2.17 find_first_of 8 1.2.18 find_last_not_of 9 1.2.19 find_last_of 9 1.2.20 get_allocator 9 1.2.21 insert 9 1.2.22 length 9 1.2.23 max_size 9 1.2.24 push_back 9 1.2.25 rbegin 9 1.2.26 rend 9 1.2.27 replace 9 1.2.28 reserve 11 1.2.29 resize 12 1.2.30 rfind 12 1.2.31 size 12 1.2.32 substr 12 1.2.33 swap 12 1.3 string的构造 12 1.4 string的重载运算符 12 1.5 string与algorithm相结合的使用 13 1.5.1 string与remove 13 1.5.2 string与unique、sort 13 1.5.3 string与search 13 1.5.4 string和find、find_if 14 1.5.5 string与copy、copy_if 14 1.5.6 string与count、count_if 15 1.6 string与wstring 15 1.6.1 简介 15 1.6.2 wstring实例 15 1.6.3 wstring与控制台 16 1.6.4 string与wstring的相互转换 17 1.7 stringC++流 22 1.7.1 C++流简介 22 1.7.2 string与iostream、fstream 22 1.8 格式化字符串 23 1.8.1 简单常用的C方法 23 1.8.2 boost的方法 23 1.8.3 stlsoft + fastformat 23 1.9 string与CString 24 二、 boost字符串算法库 24 2.1 boost字符串算法库导论 24 2.1.1 boost.algorithm.string是什么? 24 2.1.2 相关 24 2.1.3 boost.range导论 24 2.1.4 boost.regex导论 24 2.1.5 boost.algorithm.string的DNA 24 2.2 boost字符串算法解密 24 2.2.1 修剪(trim.hpp) 24 2.2.2 转换(case_conv.hpp) 24 2.2.3 判断式、断言函数(predicate.hpp)【Predicates】 24 2.2.4 查找 24 2.2.5 删除和替换 24 2.2.6 分割和组合 24 2.2.7 分词 24 2.2.8 其它 24 三、 C字符串 24 3.1 C字符串常用算法 24 3.1.1 strcpy wcscpy 24 3.1.2 strcat wcscat 24 3.1.3 strchr wcschr 24 3.1.4 strcmp wcscmp 24 3.1.5 stricmp wcsicmp 24 3.1.6 strlen wcslen 24 3.1.7 strlwr/_strlwr wcslwr/_wcslwr 24 3.1.8 strncat wcsncat 24 3.1.9 strcspn wcscspn 24 3.1.10 strdup/_strdup wcsdup/_wcsdup 24 3.1.11 strncpy wcsncpy 24 3.1.12 strpbrk wcspbrk 24 3.1.13 strrev/_strrev wcsrev/_wcsrev 24 3.1.14 strset/_strset/_strset_l wcsset/_wcsset/_wcsset_l 24 3.1.15 strstr/wcsstr 24 3.1.16 strtok/wcstok 24 3.1.17 strupr/_strupr wcsupr/_wcsup

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值