VC++中字符串转换
malong 发布于 2022-02-04

windows环境的C/C++字符串表示有以下类型,

char[],char*,string,LPSTR,LPWSTR,LPCSTR,LPCWSTR,CString,LPSTR是一个指向32位ANSI字符数组指针,而LPWSTR是一个指向64位双字节字符数组指针。

LPWSTR的定义:

typedef _Null_terminated_ CONST WCHAR *LPCWSTR, *PCWSTR;

LPSTR定义:

typedef _Null_terminated_ CHAR *NPSTR, *LPSTR, *PSTR;

1、char[]、char *转LPCWSTR或者wchar_t*

char str[100] = "hello world";
int num = MultiByteToWideChar(0,0,str,-1,NULL,0);
wchar_t *wide = new wchar_t[num];
MultiByteToWideChar(0,0,str,-1,wide,num);

2、LPCTSTR转 char *

wchar_t wstr[100] = L"hello world";
int num = WideCharToMultiByte(CP_OEMCP,NULL,wstr,-1,NULL,0,NULL,FALSE);
char *pchar = new char[num];
WideCharToMultiByte (CP_OEMCP,NULL,wstr,-1,pchar,num,NULL,FALSE);

 

 

 

 

malong
关注 私信
文章
35
关注
0
粉丝
0