放牧代码和思想
专注自然语言处理、机器学习算法
    正处于一个非常忙的阶段,抱歉不会经常回应任何联络

CHAR,TCHAR,WCHAR 三者的区别与转换

#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 三者的区别与转换

分享到:更多 ()

评论 欢迎留言

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

我的开源项目

HanLP自然语言处理包基于DoubleArrayTrie的Aho Corasick自动机