1.C++字符串与C字符串的转换:

(1)string --> char *

string str("OK");

strcpy(p,str.c_str());//p是char*

(2)char * -->string

char p[] = "OK";

string str(p);  <=>  str=p;

2.数字转化为C字符串     使用sprintf()函数:

char str[10];

int a=1234321;

sprintf(str,"%d",a);

--------------------

char str[10];

double a=123.321;

sprintf(str,"%.3f",a);

--------------------

char str[10];

int a=175;

sprintf(str,"%x",a);//10进制转换成16进制,如果输出大写的字母是sprintf(str,"%X",a)

3.C字符串转化为数字

1)使用sscanf()函数:

char str[]="1234321";

int a;

sscanf(str,"%d",&a);

.............

char str[]="123.321";

double a;

sscanf(str,"%lf",&a);

.............

char str[]="AF";

int a;

sscanf(str,"%x",&a); //16进制转换成10进制

2) 使用atoi, atol, atof:(头文件是stdlib.h)

int atoi(const char *nptr);

long atol(const char *nptr);

double atof(const char *nptr);

1.关于%f与%lf:

输入时float对应%f, double对应%lf; 输出时float和double都对应%f。

数字与字符串之间的转换以及%f与%lf的输入输出用法区别的更多相关文章

  1. C++中数字与字符串之间的转换,别人的,

    C++中数字与字符串之间的转换   1.字符串数字之间的转换 (1)string --> char *   string str("OK");   char * p = st ...

  2. C++中数字与字符串之间的转换 scanf string总结(复习必读)

    1 string的scanf读入操作 C++里面控制台输入直接使用cin操作就可以了:或者getline(istringstream,string); 字符和数字加减就是字符的ASCII码和数字直接加 ...

  3. C++中数字与字符串之间的转换

    原文地址:http://www.cnblogs.com/luxiaoxun/archive/2012/08/03/2621803.html 1.字符串数字之间的转换 (1)string --> ...

  4. C++中数字与字符串之间的转换(使用CString.Format或者sprintf)

    1.字符串数字之间的转换 (1)string --> char *   string str("OK");   char * p = str.c_str(); (2)char ...

  5. 【Go】IP地址转换:数字与字符串之间高效转换

    转载:https://blog.thinkeridea.com/201903/go/ip2long.html IP 地址库中 IP 地址的保存格式一般有两种,一种是点分十进制形式(192.168.1. ...

  6. C++中数字与字符串之间的转换(转)

    http://www.cnblogs.com/luxiaoxun/archive/2012/08/03/2621803.html 1.字符串数字之间的转换 (1)string --> char ...

  7. [C/C++] C/C++中数字与字符串之间的转换

    在C中: 方法: 1.C标准库中的sprintf, sscanf 2.C标准库还提供了 atoi, atof, atol, atoll(C++11标准) 函数将字符串转换成int,double, lo ...

  8. js 中数字与字符串之间的转换

    数字转换为字符串 var num  = 123: 1.num.toString 2."" + num 3.String(num) 将数字转化为格式化后的字符串 num.toFixe ...

  9. boost-使用format和lexical_cast实现数字和字符串之间的转换

    使用boost的format可以实现数字到string的格式化转换,boost的lexical_cast可以实现string到数值的转换,eg: #include "boost/format ...

随机推荐

  1. HDU 1116 Play on Words(并查集和欧拉回路)(有向图的欧拉回路)

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  2. centos 7.5 安装mongodb

    MongoDB安装和启动 从官网下载最新对应的版本然后解压,本文以3.6.9为例,将文件拷贝到opt目录下,然后解压: [root@localhost opt]# tar zxvf mongodb-l ...

  3. Spark分析之TaskScheduler

    TaskScheduler概述: TaskScheduler是一个可插拔任务调度接口,通过不同的SchedulerBackend进行任务的调度.主要功能如下: 1.一个TaskScheduler只为一 ...

  4. php字符串类型讲解

    PHP 支持八种原始类型(type). 四种标量类型: string(字符串) integer(整型) float(浮点型,也作 double ) boolean(布尔型) 两种复合类型: array ...

  5. spark 应用程序部署工具 spark-submit

    打包 Spark application 使用spark-submit启动Spark application spark-submit usage spark-submit option 运行模式相关 ...

  6. 转化Excel表格为php配置文件

    <?php     //建立reader对象 ,分别用两个不同的类对象读取2007和2003版本的excel文件    require("PHPExcel/Reader/Excel20 ...

  7. jquery knob旋钮插件

    <!DOCTYPE html> <html> <head> <title>jQuery Knob 尝试</title> <script ...

  8. rhel7配置tiger vnc详解 centos6配置安装vnc-server

    参考网站:http://blog.51cto.com/xjsunjie/1963463     结合  https://blog.csdn.net/wamath/article/details/760 ...

  9. J2SE 8的Lambda --- 语法

    语法例子 LambdaGrammarTest lambdaTest = new LambdaGrammarTest(); // 1. 能够推导出类型的,可以不写类型 String[] planets ...

  10. mysql find_in_set排序

    假若有给定id数组(1,9,3,5,6);查询sql语句需要安装这个顺序排序 select * from tb where id in (1,9,3,5,6) order by find_in_set ...