zoukankan      html  css  js  c++  java
  • C++字符串和数字格式转化(使用sprintf()和sscanf()函数)

    本文参考摘抄整理自:http://www.cnblogs.com/luxiaoxun/archive/2012/08/03/2621803.html

     1 #include<stdio.h>
     2 #include<iostream>
     3 using namespace std;
     4 int main(){
     5     
     6     //--------------数字转字符串,使用sprintf(char数组名,类型,数字变量名) 
     7     //整数转十进制字符串
     8     char str[10];
     9     int a=1234321;
    10     sprintf(str,"%d",a);
    11     
    12     //小数转十进制字符串 
    13     char str1[10];
    14     double b=123.321;
    15     sprintf(str1,"%.3lf",b);
    16     //十进制整数转十六进制字符串
    17     char str2[10];
    18     int c=175;
    19     sprintf(str2,"%x",c);
    20     cout<<"数字-->字符串:"<<endl;
    21     cout<<str<<endl<<str1<<endl<<str2<<endl;
    22     
    23     //----------------字符串转数字,使用sscanf()函数
    24     //字符串转整数 
    25     char str3[]="1234321";
    26     int d;
    27     sscanf(str3,"%d",&d); 
    28     
    29     //字符串转小数
    30     char str4[]="123.321";
    31     double e;
    32     sscanf(str4,"%lf",&e);
    33     //十六进制字符串转十进制整数
    34     char str5[]="AF";
    35     int f;
    36     sscanf(str5,"%x",&f); 
    37     
    38     cout<<"字符串-->数字:"<<endl;
    39     cout<<d<<endl<<e<<endl<<f<<endl;
    40 }

  • 相关阅读:
    手动档和自动档
    关于目标:骑行里程破万的感想
    JavaScript基础学习-iterable
    第一个mybatisplus
    MAVEN安装配置
    List和ArrayList的区别
    mysql安装
    Nginx的命令
    Windows Server 2008/2012/2016允许多个用户同时远程桌面
    soapui模拟桩-4 将模拟桩打包成war包
  • 原文地址:https://www.cnblogs.com/Elaine-DWL/p/6402762.html
Copyright © 2011-2022 走看看