Vc CString 和 string 类型转换集合

C_dm22 2017-01-03 10:48:31
加精
CString 转换集合

CString 转 string
可能要添加相关的头文件,CT2A 它定义在"atlconv.h"中

CString file1;
string filename ;
CT2A xx(file1);
filename = xx;

CString 转 char*

CString strRootPath;
USES_CONVERSION;
char* chRootPath = T2A(strRootPath);

CString 转 int

int MinSize = _ttoi(CString cst_MinSize);

CString 转 float

CString str_data;
float f_data;
f_data=_tstof(str_data);

/ * ------------------------------------------------------------------------------------------------------------------------ */

string 转换集合

string 转 char*
char* p = (char*)(strpath.c_str())

string 转 float

string str_data="12345";
float f_data;
f_data= atof( str_data.c_str() );

string 转 int
int n = atoi(str.c_str());
...全文
4184 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zy380526481 2017-02-10
  • 打赏
  • 举报
回复
string类的成员函数c_str()返回的是const char*类型,你强制把它转换成char*类型是很不安全的,而且在C++中应该尽量避免使用这种老式的强制类型转换语法。另外我觉得一般情况下并不需要将string类转换成char*,更多情况下需要将其转换成const char*,这种情况下直接调用c_str()函数即可。
nettman 2017-01-29
  • 打赏
  • 举报
回复
Daniel_31 2017-01-23
  • 打赏
  • 举报
回复
MARK.....
lzlichun 2017-01-21
  • 打赏
  • 举报
回复
谢谢分享!!!!
细水长流- 2017-01-19
  • 打赏
  • 举报
回复
fly4free 2017-01-11
  • 打赏
  • 举报
回复
我是这样在 CString 与 string 之间转换的: 从 CString 到 string,如果是UNICODE 工程:
  CString strSource;
  CStringA strTmp;
  std::string target;
  strTmp = strSource;
  target = strTmp.GetString();
如果是非 UNICODE 工程,下面直接 target = strSource.GetString(); 从 string 到 CString 就简单了
std::string src;
CString target;
target = src.c_str();
hugh_z 2017-01-11
  • 打赏
  • 举报
回复
66666666666666
赵4老师 2017-01-11
  • 打赏
  • 举报
回复
引用 3 楼 VisualEleven 的回复:
如果记得没错的话,T2A好像有个长度的限制,1024吧,最好WCHAR*转char*还是使用WideCharToMultiByte()函数
include\atlconv.h: #define W2A(lpw) (\ ((_lpw = lpw) == NULL) ? NULL : (\ (_convert = (lstrlenW(_lpw)+1), \ (_convert>INT_MAX/2) ? NULL : \ ATLW2AHELPER((LPSTR) alloca(_convert*sizeof(WCHAR)), _lpw, _convert*sizeof(WCHAR), _acp)))) …… #define T2A W2A …… 综上所述,T2A的长度受alloca能分配的最大字节数限制: /STACK (Stack Allocations) Home | Overview | How Do I | Linker Options The Stack Allocations (/STACK:reserve[,commit]) option sets the size of the stack in bytes. To find this option in the development environment, click Settings on the Project menu. Then click the Link tab, and click Output in the Category box. The Reserve text box (or in the reserve argument on the command line) specifies the total stack allocation in virtual memory. The default stack size is 1 MB. The linker rounds up the specified value to the nearest 4 bytes. The optional value specified in the Commit text box (or in the commit argument on the command line) is subject to interpretation by the operating system. In Windows NT, it specifies the amount of physical memory to allocate at a time. Committed virtual memory causes space to be reserved in the paging file. A higher commit value saves time when the application needs more stack space, but increases the memory requirements and possibly the startup time. Specify the reserve and commit values in decimal or C-language notation. Another way to set the size of the stack is with the STACKSIZE statement in a module-definition (.DEF) file. STACKSIZE overrides the Stack Allocations (/STACK) option if both are specified. You can change the stack after the .EXE file is built by using the EDITBIN tool.
Eleven 2017-01-11
  • 打赏
  • 举报
回复
如果记得没错的话,T2A好像有个长度的限制,1024吧,最好WCHAR*转char*还是使用WideCharToMultiByte()函数
lx624909677 2017-01-11
  • 打赏
  • 举报
回复
T2A这类的转换方发有内存泄露,在循环里不要用这个去转,另外在unicode和多字符集下,转换方式也不同
ljheee 2017-01-11
  • 打赏
  • 举报
回复
66666,之前搞VC的时候,这些类型转换真头疼。
赵4老师 2017-01-09
  • 打赏
  • 举报
回复
字符串那些事儿:BSTR-LPSTR-LPWSTR-CString-VARIANT-COleVariant-_variant_t-CComBSTR-_bstr_t http://blog.csdn.net/pizi0475/archive/2010/03/04/5346708.aspx
oyljerry 2017-01-06
  • 打赏
  • 举报
回复
多谢分享

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧