由于结构体中用到联合体(联合体需要确定分配内存分配大小)或其它因素,需要用char数组来保存字符串,但是在MFC中一般都是用CString来存放字条串。关于它们之间的转换,在VS2008中有时会出现异常情况。在MSDN是这样写的:

CString orig("Hello, World!");

// Convert to a char*
    const size_t newsize = 100;
    char nstring[newsize];
    strcpy_s(nstring, orig);

但在实际应用中,并不能通过,总会在strcpy_s()函数中出错,或者在nstring的后面跟着很多乱码尾巴。在网上查阅了一些方法。如下:

方法一:
char *p;
CString str="hello";
p=str.GetBuffer(str.GetLength());
str.ReleaseBuffer();
方法二:
CString str="hello";
char ch[20];
memcpy(ch,str,str.GetLength());
方法三:
char *ch;
CString str="hello";
ch=(LPSTR)(LPCTSTR)str;

但总达不到期望的结果。随后再在网上查,发现是Unicode字符集的问题。选择项目->项目属性(或直接按alt+F7)->配置属性,在右边找到“字符集”,将“使用Unicode字符集”改为“使用多字节字符集”。保存之后需要重新生成解决方案。这样上面的方法都可以通过并实现,但是在方法二中,最好不要使用memcpy,直接用strcpy_s(char*, CString)就可以了,因为用memcpy也会出现乱码尾巴。

如果不想改变Unicode字符集,网上也有介绍方法,但我没有试过,在此列出来供网友们参考:

CString strPath = L"adfafs主声音文件fsfsa";
int nLength = strPath.GetLength();
int nBytes = WideCharToMultiByte(CP_ACP,0,strPath,nLength,NULL,0,NULL,NULL);
char* VoicePath = new char[ nBytes + 1];
memset(VoicePath,0,nLength + 1);
WideCharToMultiByte(CP_OEMCP, 0, strPath, nLength, VoicePath, nBytes, NULL, NULL);
VoicePath[nBytes] = 0;

MFC中CString转换成char数组的问题的更多相关文章

  1. sprintf将CString转换成char[]

      在MFC中使用sprintf()函数将CString转换成char[]时,char[]只接受第一个字符 使用的是VS2008 CString name;dbName="test" ...

  2. CString转换成char*

    CString转换成char* :charSource = (char*)strSource.GetBuffer(0); 法2:charSource = (char*)strSource.GetBuf ...

  3. CString 转换成 char *

    最近用到CString类,转换成 char * 类型,下面介绍用法: 一.CString 和 LPSTR 转换: CString转换成LPSTR: 方法一:CString server; LPSTR ...

  4. MFC中cstring,string和char[]的相互转化

    int 转 CString:CString.Format("%d",int);...............................string 转 CString CSt ...

  5. CString转换成char *字符串问题

    buf = (LPSTR)(LPCTSTR)str;      ==>     buf 显示的是第一个字符 strcpy(pNumber,strNumber);      ==>    e ...

  6. MFC中CString转化为char*

    char* convertCStringToChars(CString string) { int nLength=string.GetLength(); ]; memset(c,,nLength+) ...

  7. VC2008中将CString转换成const char*的一种有效方法

    文章转载自http://blog.csdn.net/lanbing510/article/details/7425613 在Visual Studio 200X下,CString直接转换成const ...

  8. 将string转换成char*

    string 是c++标准库里面其中一个,封装了对字符串的操作  把string转换为char* 有3中方法:  1.data  如:  string str="abc";  ch ...

  9. 简析MFC中CString用作C字符串

      MFC中CString是一个方便的字符串操作的类, 然而很多函数需要传递字符指针, 这就需要进行CString和普通字符串的转换. 1.CString用作C字符串常量. 直接使用强制类型转换即可, ...

随机推荐

  1. HTML5中标记与特殊属性

    不允许写结束标记的元素有(只允许<元素/>): area.base.br.col.command.embed.hr.img.input. keygen.link.meta.param.so ...

  2. ubuntu14.04无法连接有线连接问题

    在windows系统下关闭有线网卡的关机,自动唤醒功能即可

  3. Getting started with Chrome Dev Editor

    转自:https://github.com/GoogleChrome/chromedeveditor/blob/master/doc/GettingStarted.md Installation In ...

  4. fastdfs安装

    1:安装libevent     rpm -aq |grep libevent|xargs rpm -e --nodeps     tar zxvf libevent-2.0.21-stable.ta ...

  5. No enclosing instance of type Demo is accessible. Must qualify the allocation with an enclosing instance of type Demo (e.g. x.new A() where x is an instance of Demo).

    No enclosing instance of type Demo is accessible. Must qualify the allocation with an enclosing inst ...

  6. JavaScript-CasperJs使用教程

    如果是类似12306这种网站的话, 必须使用--ssl-protocol=any --ignore-ssl-errors=true选项, 例如 casperjs --ssl-protocol=any ...

  7. (C#)Windows Shell 外壳编程系列1 - 基础,浏览一个文件夹

    1 - 基础,浏览一个文件夹 我们知道,在win32中是以外壳名字空间的形式来组织文件系统的,在外壳名字空间里的每一个对象(注)都实现了一个IShellFolder的接口,通过这个接口我们可以直接查询 ...

  8. 实现对数据进行分组小计并计算合计的实例 asp.net

    可以通过数据绑定来实现  通过union all 来实现数据库 SELECT * FROM v3_pay_list2 where ( (ought_date >= '2012-12-06') a ...

  9. 图解SQL inner join、left join、right join、full outer join、union、union all的区别

    转于:http://justcoding.iteye.com/blog/2006487 这是一篇来自Coding Horror的文章. SQL的Join语法有很多:有inner的,有outer的,有l ...

  10. iOS获取当前设备方向

    三种方式: self.interfaceOrientation [[UIApplication sharedApplication] statusBarOrientation] [[UIDevice ...