字符串/数字之间类型转换总结

来源:互联网 发布:破解软件网站 编辑:程序博客网 时间:2024/04/29 09:38

1、char向int转换

方法一:(适用于单个字符)

char ch = '6';

int num = ch - '0';   //此时num=6

方法二:(适用于字符串)

函数atoi: int atoi ( const char * str );

参数是一个char类型的数组,不能是单个char变量

char str[10] = "32352";

int num = atoi(str);

方法三:

 sscanf(str,"%d",&a);  其中str ="12234" int a

#include <stdio.h>

int main(int argc, char *argv[])

{

   int a;double d;

   char str[] = "1024";

   char strd[] = "3.1415";

    sscanf(str,"%d",&a);         //char 转 int

    sscanf(strd,"%lf",&d);             //char 转 double

   printf("%d\n",a);

   printf("%g\n",d);

    return 0;

}

2、int 向char转换

方法一:适用于单个数字

int n = 6;

char s = n +48;              //只需要将int型的值加48存储到char类型的变量中,s = '6'

方法二:

char*  itoa ( int value, char * str, int base); 将int类型的value值按照base进制转换为char存储在str数组中

注:value 整型值 str要存储的数组 base 进制

#include<stdio.h>

#include <stdlib.h>

main(void)

{

       charch[100] ;

       inta = 1000;

       itoa(a,ch, 10);              //将整数1000转换为char存储到数组中

       printf("%s\n",ch);

}

3、char向double、 long int 的转换

 

double atof ( const char * str );              //char转double

例子:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

 

int main ()

{

 double n;

 char szInput [256];

 gets ( szInput );

  n =atof (szInput );

 printf ( "%f\n" , n);

 return 0;

}

 

long int atol ( const char * str );        //char转long int

例子:

#include <stdio.h>

#include <stdlib.h>

 

int main ()

{

  long int li;

 char szInput [256];

 printf ("Enter a long number: ");

 gets ( szInput );

  li= atol (szInput);

 printf ("The value entered is %d. The double is %d.\n",i,i*2);

 return 0;

}

4、int转为string类型

 

//int转为string类型

//许永伟

//2012-3-9

 

/*方法一:使用函数itoa()

itoa(  int   value,   char  *string,   int   radix  );  

    第一个参数:你要转化的int;  

    第二个参数:转化后的char*;  

    第三个参数:你要转化的进制;

 

先对char类型的指针处理,然后赋值给string类型的变量。

 

#include<iostream>

using namespace std;

 

int main()

{

       intstart = 333335;

       stringdest;

       char*temp = new char[dest.length()];

      

       itoa(start,temp, 10);

       dest= temp;

       cout<< dest << endl;

       system("pause");

       return0;

}

*/

/////////////////////////////////////////////////////

/*

方法二:使用sprintf函数

头文件#include<stdio.h>

 

语法: intsprintf(string format, mixed [args]...);

 

返回值:字符串长度(strlen)

 

转换字符

 

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

 

% 印出百分比符号,不转换。

 

b 整数转成二进位。

 

c 整数转成对应的 ASCII 字元。

 

d 整数转成十进位。

 

f 倍精确度数字转成浮点数。

 

o 整数转成八进位。

 

s 整数转成字串。

 

x 整数转成小写十六进位。

 

X 整数转成大写十六进位。

 

#include <iostream> 

#include <string> 

using namespace std; 

int main() 

       /*用char数组

   int n = 30; 

   char c[20];

   sprintf(c, "%d", n); 

   cout << c << endl; 

   sprintf(c, "%o", n);     

   cout << c << endl; 

   sprintf(c, "%X", n);     

   cout << c << endl; 

    sprintf(c, "%c", n);     

   cout << c << endl; 

   float f = 24.678; 

   sprintf(c, "%f", f);     

   cout << c << endl; 

   sprintf(c, "%.2f",f);     

   cout << c << endl; 

   sprintf(c, "%d-%.2f",n, f); 

   cout << c << endl; 

    */

   /*

   int n = 30;

   string b;

       char*c =  new char[b.length()];

       sprintf(c,"%d", n);  //将int类型的n转化后赋值给c

       b= c;

   cout << b << endl; 

   system("pause"); 

   return 0; 

   

*/

///////////////////////////////////////////

/*

方法三:使用 stringstream

*/

#include <iostream> 

#include <string> 

#include <sstream> 

using namespace std; 

int main() 

   stringstream strStream; 

   int a = 100; 

   float f = 23.5566; 

   strStream << a ;

   string s = strStream.str(); 

   cout << s << endl;

       strStream.str("");    //清除数据a

       strStream<< f;

       stringb = strStream.str();

       cout<< b << endl; 

   system("pause"); 

   return 0; 

 

5、string 转int 类型

 

#include <iostream>

#include <string>

using namespace std;

int main(int argc, char *argv[])

{

   int a;

   string str = "1024";

    a= atoi(str.c_str());

   cout << a <<endl;

   return 0;

}

 

总结:

char/string  int /float /double

以后遇到将char类型的数组或者string中的数字转换为int float double 等时,统一使用sscanf函数。

#include <stdio.h>

#include<string>

using namespace std;

int main(void)

{

  //char s[100] = "54";               //string转化为char型指针,用c_str()函数即可

  string s = "23";

       inta = 0;

       doubleb = 0;

       floatc = 0;

 

       sscanf(s.c_str(),"%d", &a);         //转int

       sscanf(s.c_str(),"%lf", &b);        //转double

       sscanf(s.c_str(),"%f", &c);          //转float

 

       printf("%d\n",a);

       printf("%.4lf\n",b);

       printf("%.3f\n", c);

  return 0;

}

Int/float/double -> char

以后遇到将int/float/double转换为char数组时,统一使用sprintf函数。

#include <stdio.h>

int main(void)

{

  inta = 100;

 float b = 3.22;

 double c = 3.3;

 char s[10];

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

 printf("%s\n", s);

 

   sprintf(s,"%.2f", b);

  printf("%s\n", s);

 

   sprintf(s,"%.2lf", c);

  printf("%s\n", s);

  return 0;

}

Int/float/double -> string

先转换为动态开辟的char数组,然后将数组赋给string

int n = 30;

    string b;

       char *c=  new char[b.length()];

       sprintf(c,"%d", n);  //将int类型的n转化后赋值给c

       b = c;

    cout<< b << endl; 

 

0 0
原创粉丝点击