CHAR,TCHAR,WCHAR 三者的区别与转换
来源:互联网 发布:哔哩哔哩是个什么软件 编辑:程序博客网 时间:2024/05/01 22:20
#ifdef UNICODE
typedef wchar_t TCHAR;
#else
typedef unsigned char TCHAR;
#endif
typedef unsigned char CHAR;
typedef unsigned wchar_t WCHAR;
由此可以看出,CHAR实施上就是unsigned char,WCHAR为宽字符,而TCHAR根据是否支持unicode而不同。
在程序使用sizeof(TCAHR),当默认设置时,这个值是1;当定义UNICODE宏时,这个值是2。
转换函数:
//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertAnsiStringToWide()
// Desc: This is a UNICODE conversion utility to convert a CHAR string into a
// WCHAR string. cchDestChar defaults -1 which means it
// assumes strDest is large enough to store strSource
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertAnsiStringToWide( WCHAR* wstrDestination, const CHAR* strSource,
int cchDestChar )
{
if( wstrDestination==NULL || strSource==NULL )
return;
if( cchDestChar == -1 )
cchDestChar = strlen(strSource)+1;
MultiByteToWideChar( CP_ACP, 0, strSource, -1,
wstrDestination, cchDestChar-1 );
wstrDestination[cchDestChar-1] = 0;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertWideStringToAnsi()
// Desc: This is a UNICODE conversion utility to convert a WCHAR string into a
// CHAR string. cchDestChar defaults -1 which means it
// assumes strDest is large enough to store strSource
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertWideStringToAnsi( CHAR* strDestination, const WCHAR* wstrSource,
int cchDestChar )
{
if( strDestination==NULL || wstrSource==NULL )
return;
if( cchDestChar == -1 )
cchDestChar = wcslen(wstrSource)+1;
WideCharToMultiByte( CP_ACP, 0, wstrSource, -1, strDestination,
cchDestChar-1, NULL, NULL );
strDestination[cchDestChar-1] = 0;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertGenericStringToAnsi()
// Desc: This is a UNICODE conversion utility to convert a TCHAR string into a
// CHAR string. cchDestChar defaults -1 which means it
// assumes strDest is large enough to store strSource
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertGenericStringToAnsi( CHAR* strDestination, const TCHAR* tstrSource,
int cchDestChar )
{
if( strDestination==NULL || tstrSource==NULL )
return;
#ifdef _UNICODE
DXUtil_ConvertWideStringToAnsi( strDestination, tstrSource, cchDestChar );
#else
if( cchDestChar == -1 )
strcpy( strDestination, tstrSource );
else
strncpy( strDestination, tstrSource, cchDestChar );
#endif
}
//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertGenericStringToWide()
// Desc: This is a UNICODE conversion utility to convert a TCHAR string into a
// WCHAR string. cchDestChar defaults -1 which means it
// assumes strDest is large enough to store strSource
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertGenericStringToWide( WCHAR* wstrDestination, const TCHAR* tstrSource,
int cchDestChar )
{
if( wstrDestination==NULL || tstrSource==NULL )
return;
#ifdef _UNICODE
if( cchDestChar == -1 )
wcscpy( wstrDestination, tstrSource );
else
wcsncpy( wstrDestination, tstrSource, cchDestChar );
#else
DXUtil_ConvertAnsiStringToWide( wstrDestination, tstrSource, cchDestChar );
#endif
}
//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertAnsiStringToGeneric()
// Desc: This is a UNICODE conversion utility to convert a CHAR string into a
// TCHAR string. cchDestChar defaults -1 which means it
// assumes strDest is large enough to store strSource
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertAnsiStringToGeneric( TCHAR* tstrDestination, const CHAR* strSource,
int cchDestChar )
{
if( tstrDestination==NULL || strSource==NULL )
return;
#ifdef _UNICODE
DXUtil_ConvertAnsiStringToWide( tstrDestination, strSource, cchDestChar );
#else
if( cchDestChar == -1 )
strcpy( tstrDestination, strSource );
else
strncpy( tstrDestination, strSource, cchDestChar );
#endif
}
//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertAnsiStringToGeneric()
// Desc: This is a UNICODE conversion utility to convert a WCHAR string into a
// TCHAR string. cchDestChar defaults -1 which means it
// assumes strDest is large enough to store strSource
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertWideStringToGeneric( TCHAR* tstrDestination, const WCHAR* wstrSource,
int cchDestChar )
{
if( tstrDestination==NULL || wstrSource==NULL )
return;
#ifdef _UNICODE
if( cchDestChar == -1 )
wcscpy( tstrDestination, wstrSource );
else
wcsncpy( tstrDestination, wstrSource, cchDestChar );
#else
DXUtil_ConvertWideStringToAnsi( tstrDestination, wstrSource, cchDestChar );
#endif
}
- CHAR,TCHAR,WCHAR 三者的区别与转换
- char TCHAR WCHAR区别 与 转换
- WCHAR,CHAR,TCHAR的区别
- WCHAR,CHAR,TCHAR的区别
- WCHAR,CHAR,TCHAR的区别
- WCHAR,CHAR,TCHAR的区别
- char,TCHAR,WCHAR区别
- char,TCHAR,WCHAR区别
- char,TCHAR,WCHAR区别
- char,TCHAR,WCHAR区别
- char,TCHAR,WCHAR 区别
- char,TCHAR,WCHAR区别
- char,TCHAR,WCHAR区别
- char,wchar_t,WCHAR,TCHAR,ACHAR的区别
- VC++ WCHAR,CHAR,TCHAR的区别
- 【转】WCHAR,CHAR,TCHAR的区别
- char,wchar_t,WCHAR,TCHAR,ACHAR的区别
- char,wchar_t,WCHAR,TCHAR,ACHAR的区别
- 关注细节
- ubuntu 8.04 bochs 调试linux 0.11内核
- c/c++ 数字转成字符串, 字符串转成数字
- 工程中导入sqlite -sqlite 基础教程(1)
- poj1458
- CHAR,TCHAR,WCHAR 三者的区别与转换
- linux下禁止交换分区
- 《人人都是产品经理》
- android:ListView相关属性说明
- C++复制构造函数的诡异行为研究
- iPhone网络通讯之Socket篇
- 最叫人向往的甜品提拉米苏
- 使用VC/MFC创建一个线程池
- iPhone开发之UINavigationBar设置背景图片