惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
T
Threatpost
Scott Helme
Scott Helme
I
Intezer
Cloudbric
Cloudbric
Help Net Security
Help Net Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Martin Fowler
Martin Fowler
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
Google Online Security Blog
Google Online Security Blog
N
News | PayPal Newsroom
L
LINUX DO - 最新话题
罗磊的独立博客
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
GbyAI
GbyAI
月光博客
月光博客
Last Week in AI
Last Week in AI
MyScale Blog
MyScale Blog
A
Arctic Wolf
Y
Y Combinator Blog
S
SegmentFault 最新的问题
F
Full Disclosure
T
Tenable Blog
C
Cybersecurity and Infrastructure Security Agency CISA
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
The Exploit Database - CXSecurity.com
L
LangChain Blog
S
Secure Thoughts
Recorded Future
Recorded Future
C
Check Point Blog
Schneier on Security
Schneier on Security
Hugging Face - Blog
Hugging Face - Blog
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
K
Kaspersky official blog
The Register - Security
The Register - Security
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
腾讯CDC
Cyberwarzone
Cyberwarzone
P
Palo Alto Networks Blog
Hacker News: Ask HN
Hacker News: Ask HN
The GitHub Blog
The GitHub Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs
T
Threat Research - Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
博客园 - 叶小钗

博客园 - 老羽

推荐AspNetPager分页控件 SqlServer2005中分页代码测试 WM中实现Splash动画效果 自定义合并多文件协议设计 JScript中Date.getTime转.Net中的DateTime .net中处理C/C++中的位域 POOM(Pocket Outlook Object Model)开发介绍及应用(转) 多线程执行多任务的DEMO GPRS管理与创建APN拨号连接 WM中电话相关的技巧 - 老羽 - 博客园 一道面试题题解 【转】Windows Mobile控制面板程序 [转]Windows Mobile上的服务程序 Smartphone2003开发总结一:Form总结 C++常用技巧一(整理收集) [转]CString,string,char*的综合比较 [转] C++ Windows字符和字符指针类型 C++字符串函数详解[转] Smartphone2003中实现焦点在ListView控件与其它控件中切换
[转]C++ int,char,string,CString类型转换(整理总结)
老羽 · 2009-06-08 · via 博客园 - 老羽

2009-06-08 16:22  老羽  阅读(1128)  评论()    收藏  举报

#include <string> //使用C++标准库的string类时

using namespace std; //同上

#include <sstream>

#include <iostream>

#include <stdlib.h> //要将string类和int类型直接转换最好有这些包含,

//因为自己写一个转换函数比较方便,函数定义参考如下

string getstring ( const int n )

{

std::stringstream newstr;
newstr<<n;
return newstr.str();

}

string 转 CString
CString.format(”%s”, string.c_str());

用c_str()确实比data()要好.

char 转 CString
CString.format(”%s”, char*);

char 转 string
string s(char *);//只能初始化,在不是初始化的地方最好还是用assign().

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

CString 转 string
string s(CString.GetBuffer()); //GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.

《C++标准函数库》中说的有三个函数可以将字符串的内容转换为字符数组和C—string
1.data(),返回没有”\0“的字符串数组
2,c_str(),返回有”\0“的字符串数组
3,copy()

将字符转换为整数,可以使用atoi、_atoi64或atol。

CString ss=”1212.12″;
int temp=atoi(ss);


将数字转换为CString变量,可以使用CString的Format函数。
CString s;
int i = 64;
s.Format(”%d”, i)
Format函数的功能很强,值得研究一下。

CString与char*互转

///char * TO cstring
CString strtest;
char * charpoint;
charpoint=”give string a value”;
strtest=charpoint;

///cstring TO char *
charpoint=strtest.GetBuffer(strtest.GetLength());

标准C里没有string,char *==char []==string

可以用CString.Format(”%s”,char *)这个方法来将char *转成CString。要把CString转成char *,用操作符(LPCSTR)CString就可以了。

CString转换 char[100]

char a[100];
CString str(”aaaaaa”);
strncpy(a,(LPCTSTR)str,sizeof(a));