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

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
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Python中的字符串数字是两种不同的数据类型,不能直接进行运算或者混合在一起使用。如果在使用字符串数字进行操作时,会出现错误。 比如,如果我们想将一个数字字符串进行相加,Python会提示类型错误(TypeError)。例如: ```python num = 10 text = "Hello" result = num + text # TypeError: unsupported operand type(s) for +: 'int' and 'str' ``` 在这个例子中,我们将一个整数(num)与一个字符串(text)相加,但是Python会报错,因为不能对整数和字符串进行直接相加运算。 要解决这个问题,我们可以将数字转换字符串,然后再进行相加。例如: ```python num = 10 text = "Hello" result = str(num) + text # 将整数转换字符串 print(result) # 输出:10Hello ``` 在这个例子中,我们使用了 `str()` 函数将整数转换字符串,然后再与另一个字符串相加。最后输出的结果是 `10Hello`。 总结来说,Python中的字符串数字是不同的数据类型,不能直接进行运算或者混合在一起使用。如果需要将数字字符串进行操作,需要进行类型转换。 ### 回答2: Python中的字符串数字是两种不同的数据类型,它们在进行操作的时候会有一些不同的规则和限制。如果出现了字符串数字的操作错误,通常有以下几种常见的情况: 1. 字符串数字直接相加: 当我们试图将一个字符串和一个数字相加时,Python会提示错误。这是因为字符串数字属于不同的数据类型,在Python中不能直接进行这样的操作。若想将数字转换字符串,可以使用str()函数将数字转换字符串类型。 2. 使用字符串中的非数字字符进行数学运算: 如果一个字符串中包含有非数字字符(如字母、符号等),而我们试图将其转换数字进行数学计算时,Python会报错。需要确保字符串中只包含数字字符,并且可以使用内置的int()或float()函数将字符串转换为对应的整型或浮点型数字。 3. 使用非法的数字表达式: 在使用数字进行运算时,需要保证表达式的语法是正确的。例如,除数不能为零,数字不能作为变量名等。如果违反了这些规则,Python会报错并提示错误信息。 4. 使用错误的字符串操作方法: 当我们试图使用错误的字符串操作方法时,Python会报错。例如,使用了不存在的字符串方法或错误的参数。需要确保使用正确的字符串方法和正确的参数来操作字符串总结起来,当Python字符串数字报错时,我们需要仔细检查代码,确保遵守正确的语法规则和使用正确的操作方法。如果无法解决问题,可以参考错误信息来定位问题,并查阅相关的Python文档或寻求帮助。 ### 回答3: 在Python中,字符串数字是两种不同的数据类型,在进行操作时需要注意类型的匹配。如果在字符串操作中出现数字报错,可能是因为以下几种情况: 1. 类型错误:Python中的字符串操作只能在字符串之间进行,不能直接对数字进行操作。如果要对数字进行操作,需要先将数字转为字符串,再进行操作。例如: ```python num = 123 str_num = str(num) # 将数字转为字符串 result = str_num + '456' # 对字符串进行操作 ``` 2. 语法错误:在字符串操作时,需要使用正确的语法格式。如果在字符串操作中出现语法错误,Python解释器会报错。可以通过检查引号、拼接符号(+)、逗号等是否使用正确来解决问题。 3. 变量未定义:如果在字符串操作的过程中,使用了未定义的变量,Python解释器会报错。可以通过检查变量是否正确赋值或声明来解决问题。 总之,如果在Python字符串操作中出现数字报错,需要仔细检查代码,确保操作对象的数据类型正确、语法无误,并确保使用的变量都已经正确定义。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值