#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
}

http://www.hankcs.com/program/cpp/distinction_with_the_conversion_of_char_tchar_wchar_three.html

CHAR,TCHAR,WCHAR 三者的区别与转换的更多相关文章

  1. Win32 API编程:CHAR TCHAR WCHAR的区别

    #ifdef   UNICODE               typedef   wchar_t   TCHAR;     #else               typedef   unsigned ...

  2. CSS 块元素、内联元素、内联块元素三者的区别与转换

    三种元素 块元素 内联元素 内联块元素 元素之间的转换 三种元素 元素就是标签,布局中常用的有三种标签,块元素.内联元素.内联块元素. 了解这三种元素的特性,才能熟练的进行页面布局. 块元素 块元素, ...

  3. char,wchar_t,WCHAR,TCHAR,ACHAR的区别----LPCTSTR

    转自http://blog.chinaunix.net/uid-7608308-id-2048125.html 简介:这是DWORD及LPCTSTR类型的了解的详细页面,介绍了和类,有关的知识,加入收 ...

  4. Char* ,CString ,WCHAR*之间的转换

    关于Char* ,CString ,WCHAR*之间的转换问题 GDI+所有类的接口函数如果要传递字符串作为参数的话,似乎都用UNICODE串,即WCHAR*.我开始也被整得晕头转向,因为窗口编程所用 ...

  5. 关于Char* ,CString ,WCHAR*之间的转换问题

    GDI+所有类的接口函数如果要传递字符串作为参数的话,似乎都用UNICODE串,即WCHAR*.我开始也被整得晕头转向,因为窗口编程所用往往是CString,用IO流读文件数据又得到char *.得益 ...

  6. 【转载】C/C++中的char,wchar,TCHAR

    点击这里查看原文章 总体简介:由于字符编码的不同,在C++中有三种对于字符类型:char, wchar_t , TCHAR.其实TCHAR不能算作一种类型,他紧紧是一个宏.我们都知道,宏在预编译的时候 ...

  7. CString与string、char*的区别和转换

    转自:http://blog.csdn.net/luoweifu/article/details/20232379 我们在C++的开发中经常会碰到string.char*以及CString,这三种都表 ...

  8. 【转】CString与string、char*的区别和转换

    我们在C++的开发中经常会碰到string.char*以及CString,这三种都表示字符串类型,有很多相似又不同的地方,常常让人混淆.下面详细介绍这三者的区别.联系和转换: 各自的区别 char*: ...

  9. c、c++ char*和wchar*互相转换

    1. 问题描述 编写程序时通常会面对一些不同的编码格式,如Unicode和multibytes.在有关字符串的处理时尤其重要,系统编程时通常会遇到很多这样的问题,例如把wchar*的字符串转换为cha ...

随机推荐

  1. Android布局中 android:layout_gravity="bottom"为何不起作用?

    在android布局时我们有时会需要将位于LinearLayout布局中的控件放在布局底部,或者是同时想将几个控件底部对齐,此时我们自然会想到使用 android:layout_gravity=&qu ...

  2. golang之路:mac下安装go

    1.下载dkg包 2.安装 3.vim .bash_profile export GOROOT=/usr/local/goexport GOPATH=$HOME/GoglandProjects/Pro ...

  3. 安装java运行环境

    1.查看java安装版本 执行命令java -version查看已安装java运行环境信息. 2.下载JDK 到sun官网下载需要的jdk版本,地址为:http://www.oracle.com/te ...

  4. atititi.soa  微服务 区别 联系 优缺点.doc

    atititi.soa  微服务 区别 联系 优缺点.doc 1. 应用微服务的动机,跟传统巨石应用的比较1 2. 面向服务架构(SOA)  esb2 3. 微服务架构(Microservices)2 ...

  5. mysql命令行导入和导出数据

    首先打开命令窗口,输入命令:mysql -h localhost -u selffabu -p 连接成功后,进行下面的操作 MySQL中导出CSV格式数据的SQL语句样本如下: select * fr ...

  6. Xcode wifi连接真机调试

    设备环境:Mac OSX 10.12.5.iOS11.Xcode9 或以上版本 PS:这是WWDC2017的新功能,iOS11以上,Xcode9这是刚性要求.这个功能不好找,就记下来了 手机连接上Xc ...

  7. Unix编程第7章 进程环境

    准备雄心勃勃的看完APUE,但是总感觉看着看着就像进入一本字典,很多地方都是介绍函数的用法的,但是给出例子远不及函数介绍的多.而且这本书还是个大部头呢.第7章的讲的进程环境,进程是程序设计中一个比较重 ...

  8. Java 异常介绍

    Java标准库内建了一些通用的异常,这些类以 Throwable 为顶层父类.Throwable又派生出 Error 类和 Exception 类. 错误:Error类以及他的子类的实例,代表了JVM ...

  9. Myeclipse 选中高亮

    打开显示功能 选择Windows->Preferences->Java-> Editor-> Mark Occurrences ,勾选选项.这时,当你单击一个元素的时候,代码中 ...

  10. Sum of Remainders(数学题)

    F - Sum of Remainders Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I ...