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
}
http://www.hankcs.com/program/cpp/distinction_with_the_conversion_of_char_tchar_wchar_three.html
CHAR,TCHAR,WCHAR 三者的区别与转换的更多相关文章
- Win32 API编程:CHAR TCHAR WCHAR的区别
#ifdef UNICODE typedef wchar_t TCHAR; #else typedef unsigned ...
- CSS 块元素、内联元素、内联块元素三者的区别与转换
三种元素 块元素 内联元素 内联块元素 元素之间的转换 三种元素 元素就是标签,布局中常用的有三种标签,块元素.内联元素.内联块元素. 了解这三种元素的特性,才能熟练的进行页面布局. 块元素 块元素, ...
- char,wchar_t,WCHAR,TCHAR,ACHAR的区别----LPCTSTR
转自http://blog.chinaunix.net/uid-7608308-id-2048125.html 简介:这是DWORD及LPCTSTR类型的了解的详细页面,介绍了和类,有关的知识,加入收 ...
- Char* ,CString ,WCHAR*之间的转换
关于Char* ,CString ,WCHAR*之间的转换问题 GDI+所有类的接口函数如果要传递字符串作为参数的话,似乎都用UNICODE串,即WCHAR*.我开始也被整得晕头转向,因为窗口编程所用 ...
- 关于Char* ,CString ,WCHAR*之间的转换问题
GDI+所有类的接口函数如果要传递字符串作为参数的话,似乎都用UNICODE串,即WCHAR*.我开始也被整得晕头转向,因为窗口编程所用往往是CString,用IO流读文件数据又得到char *.得益 ...
- 【转载】C/C++中的char,wchar,TCHAR
点击这里查看原文章 总体简介:由于字符编码的不同,在C++中有三种对于字符类型:char, wchar_t , TCHAR.其实TCHAR不能算作一种类型,他紧紧是一个宏.我们都知道,宏在预编译的时候 ...
- CString与string、char*的区别和转换
转自:http://blog.csdn.net/luoweifu/article/details/20232379 我们在C++的开发中经常会碰到string.char*以及CString,这三种都表 ...
- 【转】CString与string、char*的区别和转换
我们在C++的开发中经常会碰到string.char*以及CString,这三种都表示字符串类型,有很多相似又不同的地方,常常让人混淆.下面详细介绍这三者的区别.联系和转换: 各自的区别 char*: ...
- c、c++ char*和wchar*互相转换
1. 问题描述 编写程序时通常会面对一些不同的编码格式,如Unicode和multibytes.在有关字符串的处理时尤其重要,系统编程时通常会遇到很多这样的问题,例如把wchar*的字符串转换为cha ...
随机推荐
- 分类--ROC 和曲线下面积
ROC 曲线(接收者操作特征曲线)是一种显示分类模型在所有分类阈值下的效果的图表.该曲线绘制了以下两个参数: 真正例率 假正例率 真正例率 (TPR) 是召回率的同义词,因此定义如下: $$TPR = ...
- redis 的安装与启动
1.redis介绍 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. Redis 是一个高性能的key-val ...
- iOS lipo 移除i386 x86_64两个平台
由于 iOS 编译的特殊性,为了方便开发者使用,我们将 i386 x86_64 armv7 arm64 几个平台都合并到了一起, 所以使用动态库上传appstore时需要将i386 x86_64两个平 ...
- Viewer 是一款强大的 jQuery 图像浏览插件。
Viewer 是一款强大的 jQuery 图像浏览插件. 主要功能: 支持选项 支持方法 支持事件 支持触摸 支持移动 支持缩放 支持旋转 支持键盘 跨浏览器支持 链接: viewer的官方演示,及g ...
- UML类图中的关系表示
UML类图中的关系和表示方法 类图是用来描述程序中的类以及它们之间的关系的,使用类图可以帮助我们简化对系统的理解.在UML类图中比较常见的关系有六种,它们分别是:依赖.关联.聚合.组合.泛化.实现,这 ...
- readonly const关键字
readonly 关键字与 const 关键字不同. const 字段只能在该字段的声明中初始化. readonly 字段可以在声明或构造函数中初始化. 因此,根据所使用的构造函数,readonly ...
- line-height:0的使用
在这里不介绍line-height的概念 建议看一下张鑫旭 写的line-height 文章 这里只说 用到的一些小地方 div中img有间隙的解决方案就是用到了这个小小的属性 那么请看方案 方案 ...
- UISegmentedControl 功能简单 分析
UISegmentedControl类似于UIButton,它可以提供多个选择操作,响应事件,但具有很大的局限性,我们更多的是使用自定义的,不过在这里还是介绍下它的基本用法. NSArray *seg ...
- Java线程面试题:设计四个线程,其中两个线程每次对 j 加 1,另外两个每次对 j 减 1,程序如下。
package thread; /** * Java线程面试题 * @author zhongfg * @date 2015-06-16 */ public class ThreadInterview ...
- ios开发:如何加载大量图片 相册示例
本文转载至 http://www.cnblogs.com/xiongqiangcs/archive/2013/06/13/3134486.html 1. Create a NSOperationQ ...