zoukankan      html  css  js  c++  java
  • 数字与字符串之间的转换以及%f与%lf的输入输出用法区别

    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。

  • 相关阅读:
    c# 基础算法(一) 九九乘法
    万能模糊查询SQL
    C#通过连接ODBC的方式调用存储过程
    《从设计到模式》学习笔记part1
    C#知识归纳
    Python之路
    Tomcat优化
    Zabbix 3.0 + Nginx + Mariadb
    Spark DataFrame ETL教程
    Python连接presto
  • 原文地址:https://www.cnblogs.com/jiu0821/p/4161764.html
Copyright © 2011-2022 走看看