1. c++中string到int的转换

1) 在C标准库里面,使用atoi:

#include <cstdlib> 
#include <string>

std::string text = "152"; 
int number = std::atoi( text.c_str() ); 
if (errno == ERANGE) //可能是std::errno 

 //number可能由于过大或过小而不能完全存储 

else if (errno == ????) 
//可能是EINVAL 

 //不能转换成一个数字 
}

2) 在C++标准库里面,使用stringstream:(stringstream 可以用于各种数据类型之间的转换)

#include <sstream> 
#include <string>

std::string text = "152"; 
int number; 
std::stringstream ss;

ss << text;//可以是其他数据类型
ss >> number; //string -> int
if (! ss.good()) 

//错误发生 
}

ss << number;// int->string
string str = ss.str(); 
if (! ss.good()) 

 //错误发生 
}

3) 在Boost库里面,使用lexical_cast:

#include <boost/lexical_cast.hpp> 
#include <string>

try 

 std::string text = "152"; 
 int number = boost::lexical_cast< int >( text ); 

catch( const boost::bad_lexical_cast & ) 

 //转换失败 
}

2.string 转 CString
CString.format(”%s”, string.c_str());
用c_str()确实比data()要好;

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

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

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

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

7.字符串的内容转换为字符数组和C—string
(1)  data(),返回没有”\0“的字符串数组
(2)  c_str(),返回有”\0“的字符串数组
(3)  copy()

8.CString与int、char*、char[100]之间的转换

(1) CString互转int

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

void CStrDlg::OnButton1()
{
   CString
   ss=”1212.12″;
   int temp=atoi(ss);
   CString aa;
   aa.Format(”%d”,temp);
   AfxMessageBox(”var is ” + aa);
}

(2) CString互转char*

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

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

(3) 标准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));

转自:http://www.cnblogs.com/sqzxcv/archive/2009/11/30/1613414.html

C++ 中int,char,string,CString类型转换的更多相关文章

  1. JAVA中int、String的类型转换

    int -> String int i=12345;String s="";第一种方法:s=i+"";第二种方法:s=String.valueOf(i); ...

  2. [转] java中int,char,string三种类型的相互转换

    原文地址:http://blog.csdn.net/lisa0220/article/details/6649707 如何将字串 String 转换成整数 int? int i = Integer.v ...

  3. java中int,char,string三种类型的相互转换

    如何将字串 String 转换成整数 int? int i = Integer.valueOf(my_str).intValue(); int i=Integer.parseInt(str); 如何将 ...

  4. Java 中int、String的类型转换

    int -> String int i=12345;String s="";第一种方法:s=i+"";第二种方法:s=String.valueOf(i); ...

  5. java中的BigDecimal和String的相互转换,int和String的类型转换,Integer类和String相互转换

    一: /*由数字字符串构造BigDecimal的方法 *设置BigDecimal的小数位数的方法 */ 注:BigDecimal在数据库中存的是number类型. import java.math.B ...

  6. c#中 uint--byte[]--char[]--string相互转换汇总

    原文:c#中 uint--byte[]--char[]--string相互转换汇总 在在做一些互操作的时候往往需要一些类型的相互转换,比如用c#访问win32api的时候往往需要向api中传入DWOR ...

  7. 计算机基础--Java中int char byte的关系

    计算机基础--Java中int char byte的关系 重要:一个汉字占用2byte,Java中用char(0-65535 Unicode16)型字符来存字(直接打印输出的话是字而非数字),当然要用 ...

  8. C++中int与string的转化

    C++中int与string的转化 int本身也要用一串字符表示,前后没有双引号,告诉编译器把它当作一个数解释.缺省情况下,是当成10进制(dec)来解释,如果想用8进制,16进制,怎么办?加上前缀, ...

  9. java和js中int和String相互转换常用方法整理

    java中int和String的相互转换常用的几种方法: int  > String int i=10;String s="";第一种方法:s=i+""; ...

  10. STL_string.【转】C++中int、string等常见类型转换

    ZC:#include <sstream> ZC:貌似还有 istringstream 和 ostringstream ... https://www.cnblogs.com/gaobw/ ...

随机推荐

  1. rabin 素性检验 随机化算法

    #include <cstdio> #include <cstdlib> #include <ctime> typedef long long int LL; in ...

  2. Codeforces Round #157 (Div. 2)

    A. Little Elephant and Chess 模拟. B. Little Elephant and Magic Square 枚举左上角,计算其余两个位置的值,在\(3\times 3\) ...

  3. Channel States

    Introduction A channel (a call) will go through many different states during its lifetime. Here we w ...

  4. JSBinding / Memory Management (GC)

    C# and JavaScript both have Garbage Collection (GC). They should not conflict with each other. Class ...

  5. 使用xhprof分析php性能

    今天偶然发现 xhprof可以远程分析php代码性能,大致步骤如下 1.  进入 xhprof , 点击右上角注册 并 登陆, 网站左侧解释了如何在本地安装测试xhprof, 我用的是右侧的图表模式, ...

  6. Docker run命令详解 转

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 Usage: doc ...

  7. npm以及gulp相关操作

    在工作流相关的第一篇博客中,我们安装了nodejs的环境,那么nodejs自带的npm是一个功能十分强大的管理器,它已经不仅仅是局限于nodejs的版本管理器了,那么当现在我们可以通过npm来下载我们 ...

  8. Emmet 真是个好东西

    他的官网:http://docs.emmet.io/ 给广大程序员节省时间 #page>div.logo+ul#navigation>li*5>a{Item $}生产 <div ...

  9. 基于SVG的web页面图形绘制API介绍

    转自:http://blog.csdn.net/jia20003/article/details/9185449 一:什么是SVG SVG是1999由W3C发布的2D图形描述语言,纯基于XML格式的标 ...

  10. Google Tensorflow 源码编译(一):Protobuf<v3.0.0-alpha-3>

    这几天终于把tensorflow安装上了,中间遇到过不少的问题,这里记录下来.供大家想源码安装的参考. 安装环境:POWER8处理器,Docker容器Ubuntu14.04镜像. Build Prot ...