(C/C++) string / *char / int 基本轉換
網路上有許 string / *char / integer 基本轉換方式
string 與 *char 互相轉換的方法
/* string to *char */
string ssbuf1 = "string temp";
char *cc_buf1 = (char*)ssbuf1.c_str();
cout << cc_buf1 << endl; /* *char to string */
char *cc_buf2 = "string data";
string ssbuf2;
strcpy((char*)ssbuf2.c_str(), cc_buf2);
cout << ssbuf2.c_str() << endl;
int to *char / string
/* int to string */
int num1 = ;
string ssbuf3;
stringstream ssbuf1_stream;
ssbuf1_stream << num1;
ssbuf3 = ssbuf1_stream.str();
cout << ssbuf3.c_str() << endl; /* int to *char */
int num2 = ;
char *cc_buf3 = (char*)malloc( * sizeof(char));
_itoa(num2, cc_buf3, );
cout << cc_buf3 << endl; /* int to string */
int num4 = 0x55;
string ssbuf4 = to_string(num4);
cout << ssbuf4 << endl; /* int to hex string */
int num5 = 0x66;
string ssbuf5;
_itoa(num5, (char*)ssbuf5.c_str(), );
cout << ssbuf5.c_str() << endl;
*char to int
/* *char to int */
char *cc_buf4 = "";
int num3;
num3 = atoi(cc_buf4);
cout << num3 << endl;
其中 atoi / _itoa 兩個函數由 stdlib.h 提供是滿常見的轉換方式,另外還有 atof 提供浮點數轉換
(C/C++) string / *char / int 基本轉換的更多相关文章
- string,char*,int 之间的转化
c++中经常遇到string,char*,int之间的相互转化,今天就来整理一下. 以下是转载并修改的内容: 以下是常用的几种类型互相之间的转换 string 转 int先转换为char*,再使用at ...
- C++ 中 string, char*, int 类型的相互转换
一.int 1.int 转换成 string 1) to_string函数 —— c++11标准增加了全局函数std::to_string: string to_string (int val); s ...
- C语言中string char int类型转换
C语言中string -- ::) 转载 ▼ 标签: 操作符 int char c语言 类型转换 分类: C/Cpp ,char型数字转换为int型 "; printf(]-');//输出结 ...
- C++中int,float,string,char*的转换(待续)
//float转string char a[100]; float b = 1.234; sprintf(a, "%f", b); string result(a); //int转 ...
- cocos2d-x类型转换(CCstring int string char UTF-8互转)
在做数据转换时,最好包含以下头文件 #include <iostream> #include <cmath> #include <string> #include ...
- 类型转换(CCstring int string char UTF-8互转)
在做数据转换时,最好包含以下头文件 #include <iostream> #include <cmath> #include <string> #include ...
- (c++) int 转 string,char*,const char*和string的相互转换
一.int 和string的相互转换 1 int 转化为 string c++ //char *itoa( int value, char *string,int radix); // 原型说明: / ...
- (转)CString,int,string,char*之间的转换
CString,int,string,char*之间的转换http://www.cnblogs.com/greatverve/archive/2010/11/10/cstring-int-string ...
- MFC/C++/C中字符类型CString, int, string, char*之间的转换
1 CString,int,string,char*之间的转换 string 转 CString CString.format("%s", string.c_str()); cha ...
随机推荐
- jQuery validator 增加多个模板
今天学了jquery validator 可以增加多个模板,而不用写长长的js代码.废话少说,直接上例子 这段是要添加的模板 上面是把模板部分是要重复增加多个的部分,需独立出来,用textarea标签 ...
- [C#] == VS Equals
声明:本篇博客翻译自:https://www.codeproject.com/Articles/1111680/equalsequals-VS-Equals-in-Csharp 由于水平(技术水平+英 ...
- 命令行传递参数并排序 AS实现加法
题目:从命令行输入参数并进行排序 1.实验准备 Integer提供了能在 int 类型和 String 类型之间互相转换的方法,还提供了处理 int 类型时非常有用的其他一些常量和方法. static ...
- Python + selenium + unittest装饰器 @classmethod
前言 前面讲到unittest里面setUp可以在每次执行用例前执行,这样有效的减少了代码量,但是有个弊端,比如打开浏览器操作,每次执行用例时候都会重新打开,这样就会浪费很多时间. 于是就想是不是可以 ...
- Solr相似度名词:VSM(Vector Space Model)向量空间模型
最近想学习下Lucene ,以前运行的Demo就感觉很神奇,什么原理呢,尤其是查找相似度最高的.最优的结果.索性就直接跳到这个问题看,很多资料都提到了VSM(Vector Space Model)即向 ...
- 用pillow和 opencv做透明通道的两图混全(blend)
from PIL import Image as image foreground = image.open("donkey.png") background = image.op ...
- 使用Toolbar + DrawerLayou实现菜单侧滑,改变toolbar左上角图标
侧边栏具体实现可以参照http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0303/2522.html getSupportActio ...
- Net特性类Description了解下
NET特性类都有个特点类名+Attribute,继承基类Attribute,我们看下微软自带的特性类:DescriptionAttribute namespace System.ComponentMo ...
- C#Encoding
1.Encoding (1).如何生成一个Encoding即一种编码 Encoding位于System.Text命名空间下,是一个抽象类,它的派生类如下图: 要实例化一个Encoding一共有以下两种 ...
- C# print pos winform
先将pos机设置为默认 控制面板->打印机和传真->右键->服务器属性 首先创建 ClassPrint 对象 using System; using System.Drawing; ...