網路上有許 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 基本轉換的更多相关文章

  1. string,char*,int 之间的转化

    c++中经常遇到string,char*,int之间的相互转化,今天就来整理一下. 以下是转载并修改的内容: 以下是常用的几种类型互相之间的转换 string 转 int先转换为char*,再使用at ...

  2. C++ 中 string, char*, int 类型的相互转换

    一.int 1.int 转换成 string 1) to_string函数 —— c++11标准增加了全局函数std::to_string: string to_string (int val); s ...

  3. C语言中string char int类型转换

    C语言中string -- ::) 转载 ▼ 标签: 操作符 int char c语言 类型转换 分类: C/Cpp ,char型数字转换为int型 "; printf(]-');//输出结 ...

  4. C++中int,float,string,char*的转换(待续)

    //float转string char a[100]; float b = 1.234; sprintf(a, "%f", b); string result(a); //int转 ...

  5. cocos2d-x类型转换(CCstring int string char UTF-8互转)

    在做数据转换时,最好包含以下头文件 #include <iostream> #include <cmath> #include <string> #include  ...

  6. 类型转换(CCstring int string char UTF-8互转)

    在做数据转换时,最好包含以下头文件 #include <iostream> #include <cmath> #include <string> #include  ...

  7. (c++) int 转 string,char*,const char*和string的相互转换

    一.int 和string的相互转换 1 int 转化为 string c++ //char *itoa( int value, char *string,int radix); // 原型说明: / ...

  8. (转)CString,int,string,char*之间的转换

    CString,int,string,char*之间的转换http://www.cnblogs.com/greatverve/archive/2010/11/10/cstring-int-string ...

  9. MFC/C++/C中字符类型CString, int, string, char*之间的转换

    1 CString,int,string,char*之间的转换 string 转 CString CString.format("%s", string.c_str()); cha ...

随机推荐

  1. [Jenkins]怎样自定义发出邮件的 From Email Address 和 From Name

    在Jenkins上建了一个执行SoapUI的task,想要自定义发送邮件的地址和姓名,怎么办呢? 在Editable Email Notification里面添加Pre-send Script 脚本如 ...

  2. ImageView.src的png图标变形问题

    图标,必须是png-24输出,如果是png-8输出,则失真.

  3. Java设计模式(4)——单例模式

    转载:http://wiki.jikexueyuan.com/project/java-design-pattern/singleton-pattern.html 单例模式根据实例化对象时机的不同分为 ...

  4. 解决Error running 'index.jsp : Address localhost:1099 is already in use的方法

    晚上在idea中  启动服务器一次后 正常运行,但是改了注解配置后  报错,报错为Error running 'index.jsp : Address localhost:1099 is alread ...

  5. 【转】java.sql.SQLException: statement is closed语句被关闭 druid连接池报错

    我之前在用druid 1.0.28版本也出现过这个问题, 现象就是: 报这个错的时候, 往往会出现在一条毫无错误的sql执行上报错,  sql放到数据库上执行或者单独拎出来执行完全没问题, 但是为什么 ...

  6. Get Current LOV Query SQL

    --3 click the lov object activing last query address.  SELECT T.SQL_TEXT    FROM V$SQLTEXT_WITH_NEWL ...

  7. Windows Phone Update3 (新分辨率 1080 x 1920 不会影响到现有WP8应用)

    更新内容: Update 3 OS version: 8.0.10501.127 or 8.0.10512.142* Accessibility. We've made several improve ...

  8. Thread in depth 1: The basic

    Every single thread has the follow elements: Execution Context:Every thread has a execution context ...

  9. spark on yarn模式下配置spark-sql访问hive元数据

    spark on yarn模式下配置spark-sql访问hive元数据 目的:在spark on yarn模式下,执行spark-sql访问hive的元数据.并对比一下spark-sql 和hive ...

  10. h5页面宽度设置7.5rem

    function ready() { var u = navigator.userAgent; var winW = document.documentElement.clientWidth; if ...