一、LPSTR LPCSTR LPTSTR LPCTSTR等

确定的类型:
LPSTR = CHAR * = char *
LPCSTR = const CHAR * = char * //c意为const 
不确定类型(可变型):
LPTSTR = LPWSTR = WCHAR * = wchar_t * //(Unicode编码)
= LPSTR = CHAR * = char * //(多字节编码) TCHAR = wchar_t //Unicode编码
= char //多字节编码  

二、CString与char *

  • 多字节环境:CString等价于const char *,因此,在const char *的参数下可以直接使用CString,但是不能用于char *。

例:

void show(const char * name)
{
AfxMessageBox(name);
}
......... CString name("luotian");
show(name); //可以通过

CString可以智能化
在多字节环境下无论怎么赋值都是ASCII编码,比如:CString a(L"luotian"),加上L修饰,CString依然将它进行ASCII编码。
在Unicode环境下,无论怎么赋值都是Unicode编码,比如:CString a("luotian"),不加L修饰依然可以通过。

  • CString与LPCTSTR

在函数参数需要LPTSTR,LPCTSTR等等时,CString变量可以直接使用.

//例如:在Unicode环境下:
CString a("Luotian");
MessageBox(a);
  • CString->LPTSTR

//强制类型转换
CString a("Luotian");
LPTSTR p = (LPTSTR)(LPCTSTR)a; //用CString的GetBuffer()方法
LPTSTR p = a.GetBuffer();
  • LPTSTR->CString

可以直接赋值操作.

三、char * 与 wchar_t *的互换(重点

  • ASCII->Unicode

LPSTR source ="Luotian";//源串
size_t len = strlen(source) + ;
size_t converted = ;
LPTSTR dest= (LPTSTR)malloc(len*sizeof(WCHAR)); mbstowcs_s(&converted,dest,len,source,_TRUNCATE);  
  • Unicode->ASCII
#define WChar2Char(lpWideCharStr,cbWideChar,lpMultiByteStr,cbMultiByte) \
WideCharToMultiByte(CP_ACP, , lpWideCharStr, cbWideChar, lpMultiByteStr, cbMultiByte, NULL, NULL)
例:
WChar2Char(source,-,dest,cb字节数);
TCHAR dest[];
LPCWSTR source=L"Luotian";
WChar2Char(source, -, dest, );

四、string和wstring

  • string->const char *(LPCSTR)和wstring->const wchar_t * (等价于LPCTSTR)

//采用.c_str()方法;
string a("Luotian");
const char * p = a.c_str();
  • char * p->string

char * p = "luotian";
string a=p;
//string和CString的一个区别在于:函数参数需要LPTSTR时,CString可以直接用,而string不行。

wchar_t,char,string,wstring等的总结的更多相关文章

  1. wchar_t char string wstring 之间的转换

    wchar_t char string wstring 之间的转换 转:http://blog.csdn.net/lbd2008/article/details/8333583 在处理中文时有时需要进 ...

  2. 深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换 [转]

    本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下. #ifndef USE_H_ #define USE_H_ # ...

  3. 深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换

    本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下-复制代码 代码如下:    #ifndef USE_H_     ...

  4. wchar_t与char、wstring与string的相互转换

    个人倾向于使用优秀的开源库做这个. 最近使用boost进行转换,代码极其简单: boost::filesystem::path src(wchar_t); char = src.string().c_ ...

  5. CString 转 char*; wstring 转 string

    1. CString  转 char* ); CString name; name.Format(_T("bookUC%d.txt"),m_ID); std::wstring _n ...

  6. C++的中英文字符串表示(string,wstring)

    在C++中字符串类的string的模板原型是basic_string template <class _Elem, class traits = char_traits<_Elem> ...

  7. C++的中英文字符串表示(string,wstring),使用wcout.imbue(std::locale("chs"));本地化解析编码

    在C++中字符串类的string的模板原型是basic_string template <class _Elem, class traits = char_traits<_Elem> ...

  8. C++字符类型总结区别wchar_t,char,WCHAR

    转至:http://www.360doc.com/content/12/0807/01/9290626_228750141.shtml 1.区别wchar_t,char,WCHAR ANSI:即 ch ...

  9. c++ 字符类型总结区别wchar_t,char,WCHAR(转)

    1.区别wchar_t,char,WCHAR ANSI:即 char,可用字符串处理函数:strcat( ),strcpy( ), strlen( )等以str打头的函数.   UNICODE:wch ...

随机推荐

  1. 使用SQL语句创建数据库2——创建多个数据库文件和多个日志文件

    在matser数据库下新建查询,输入的命令如下: USE master GOCREATE DATABASE E_MarketON PRIMARY--主文件组( NAME ='E_Market_data ...

  2. vs的【warning C4996:'fopen': This function or variable may be unsafe】解决方案

    编译警告:warning C4996 与 Security Enhancements in the CRT 将过去的工程用VS2005打开的时候.你有可能会遇到一大堆的警告:warning C4996 ...

  3. 9.Mysql字符集

    9.字符集9.1 字符集概述 字符集就是一套文字符号及其编码.比较规则的集合. ASCII(American Standard Code for Information Interchange)字符集 ...

  4. Liunx cal

    1.命令格式: cal [参数][月份][年份] 2.命令功能: 用于查看日历等时间信息,如只有一个参数,则表示年份(1-9999),如有两个参数,则表示月份和年份 3.命令参数: -1 显示一个月的 ...

  5. ROC曲线 Receiver Operating Characteristic

    ROC曲线与AUC值   本文根据以下文章整理而成,链接: (1)http://blog.csdn.net/ice110956/article/details/20288239 (2)http://b ...

  6. 1.3 Java中的标识符和关键字

    1.标识符 含义:标识符用于给程序中的类.变量.方法命名的符号. 组成:数字(0-9).字母(a-z)(A-Z).下划线(_).美元符号$. 命名规则:1.数字不能够作为命名符号的开头 2.不能够使用 ...

  7. Delphi--最强大的开发工具(欢迎转载)

    最强大的开发工具 Delphi 目录 --------------------------------------------------------------------------- 前言 De ...

  8. 如何查看xmtb项目接口

    http://api.xmtb.com/?act=jucheng&op=get_show

  9. libusb开发

    转:https://www.cnblogs.com/ele-eye/p/3261970.html

  10. How to execute sudo command in remote host via SSH

    Question: I have an interactive shell script, that at one place needs to ssh to another machine (Ubu ...