網路上有許 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. 20155230 2016-2017-2 《Java程序设计》第七周学习总结

    20155230 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 世界时:在1972年引入UTC之前,GMT与UT是相同的 格林威治标准时间(GMT),现已不 ...

  2. 服务器上如何再另外添加一个E盘

    我的电脑按右键,选择管理..然后在左边选择磁盘管理,然后对着C盘或者D盘按右键,选择压缩卷,等一下下,就会出现个框框让你选择压缩多少,然后多了一个可用空间,再把它分成一个分区就OK拉 步骤:对可用空间 ...

  3. cpuinfo

    在Linux系统中,如何详细了解CPU的信息呢? 当然是通过cat /proc/cpuinfo来检查了,但是比如几个物理CPU/几核/几线程,这些问题怎么确定呢? 经过查看,我的开发机器是1个物理CP ...

  4. Linux下编译busybox时出现的问题

    编译busybox的时候出现了一个问题: sync.c:(.text.sync_main+0x78): undefined reference to `syncfs' collect2: ld ret ...

  5. swoole实现websocket推送

    环境配置:      swoole 1.9.3.centos6.5(虚拟机).PHP7.01   思路:      ①通过server中的collections取出fd      ②写一个admin. ...

  6. HBase-1.2.1和Phoenix-4.7.0分布式安装指南

    目录 目录 1 1. 前言 2 2. 概念 2 2.1. Region name 2 3. 约定 2 4. 相关端口 3 5. 下载HBase 3 6. 安装步骤 3 6.1. 修改conf/regi ...

  7. JS+MySQL获取 京东 省市区 地区

    采集了一下JD的省市区地区 (非常简单,只是做个记录) 1.建表:account_area   2.进入页面: https://reg.jd.com/reg/company 在浏览器(Firefox) ...

  8. [c# 20问] 1. 何时使用class与struct

    POINTS struct为可以包含数据和函数的值类型 struct为值类型所以不需要堆(heap)而是在栈(stack)上分配空间 struct将数据直接存在struct中,而class只存引用类型 ...

  9. Axure8.0入门级基础操作到高手产品经理

    宅男下载了一个Axure8.0.从来没有用过,那就从最基础的了解一下呗. 1.添加元件,选择元件设置样式(右侧对话框中)略!!!!! 2.按住ctrl键,用鼠标点选元件,可以任意旋转3.点元件上的黑圆 ...

  10. Git Note - git tag

    git tag is used to create labels, usually for version numbers. Format: git tag <TagName> <r ...