java中int强转char和Str_(java/C++)中string和int类型互相转化

java中string和int互相转化

1 如何将字串 String 转换成整数 int?

A. 有两个方法:

1). int i = Integer.parseInt([String]); 或

i = Integer.parseInt([String],[int radix]);

2). int i = Integer.valueOf(my_str).intValue();

注: 字串转成 Double, Float, Long 的方法大同小异.

2 如何将整数 int 转换成字串 String ?

A. 有叁种方法:

1.) String s = String.valueOf(i);

2.) String s = Integer.toString(i);

3.) String s = "" + i;

注: Double, Float, Long 转成字串的方法大同小异.

int -> String

int i=12345;

String s="";

第一种方法:s=i+"";

第二种方法:s=String.valueOf(i);

这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?

String -> int

s="12345";

int i;

第一种方法:i=Integer.parseInt(s);

第二种方法:i=Integer.valueOf(s).intValue();

这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?

以下是答案:

第一种方法:s=i+""; //会产生两个String对象

第二种方法:s=String.valueOf(i); //直接使用String类的静态方法,只产生一个对象

第一种方法:i=Integer.parseInt(s);//直接使用静态方法,不会产生多余的对象,但会抛出异常

第二种方法:i=Integer.valueOf(s).intValue();//Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),也会抛

异常,但会多产生一个对象

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

1如何将字串 String 转换成整数 int?

A. 有两个方法:

1). int i = Integer.parseInt([String]); 或

i = Integer.parseInt([String],[int radix]);

2). int i = Integer.valueOf(my_str).intValue();

注: 字串转成 Double, Float, Long 的方法大同小异.

2 如何将整数 int 转换成字串 String ?

A. 有叁种方法:

1.) String s = String.valueOf(i);

2.) String s = Integer.toString(i);

3.) String s = "" + i;

注: Double, Float, Long 转成字串的方法大同小异.

JAVA数据类型转换

关键字   类型转换

这是一个例子,说的是JAVA中数据数型的转换.供大家学习引

package cn.com.lwkj.erts.register;

import java.SQL.Date;

public class TypeChange {

public TypeChange() {

}

//change the string type to the int type

public static int stringToInt(String intstr)

{

Integer integer;

integer = Integer.valueOf(intstr);

return integer.intValue();

}

//change int type to the string type

public static String intToString(int value)

{

Integer integer = new Integer(value);

return integer.toString();

}

//change the string type to the float type

public static float stringToFloat(String floatstr)

{

Float floatee;

floatee = Float.valueOf(floatstr);

return floatee.floatValue();

}

//change the float type to the string type

public static String floatToString(float value)

{

Float floatee = new Float(value);

return floatee.toString();

}

//change the string type to the sqlDate type

public static java.sql.Date stringToDate(String dateStr)

{

return java.sql.Date.valueOf(dateStr);

}

//change the sqlDate type to the string type

public static String dateToString(java.sql.Date datee)

{

return datee.toString();

}

public static void main(String[] args)

{

java.sql.Date day ;

day = TypeChange.stringToDate("2003-11-3");

String strday = TypeChange.dateToString(day);

System.out.println(strday);

}

}

JAVA中常用数据类型转换函数

虽然都能在JAVA API中找到,整理一下做个备份。

C++ int与string的转化

int本身也要用一串字符表示,前后没有双引号,告诉编译器把它当作一个数解释。缺省情况下,是当成10进制(dec)来解释,如果想用8进制,16进制,怎么办?加上前缀,告诉编译器按照不同进制去解释。8进制(oct)---前缀加0,16进制(hex)---前缀加0x或者0X。

string前后加上双引号,告诉编译器把它当成一串字符来解释。

注意:对于字符,需要区分字符和字符表示的数值。比如:char a = 8;char b = '8',a表示第8个字符,b表示字符8,是第56个字符。

int转化为string

1、使用itoa(int to string)

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

// 原型说明:

// value:欲转换的数据。

// string:目标字符串的地址。

// radix:转换后的进制数,可以是10进制、16进制等。

// 返回指向string这个字符串的指针

int aa = 30;

char c[8];

itoa(aa,c,16);

cout<

注意:itoa并不是一个标准的C函数,它是Windows特有的,如果要写跨平台的程序,请用sprintf。

2、使用sprintf

// int sprintf( char *buffer, const char *format, [ argument] … );

//参数列表

// buffer:char型指针,指向将要写入的字符串的缓冲区。

// format:格式化字符串。

// [argument]...:可选参数,可以是任何类型的数据。

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

int aa = 30;

char c[8];

int length = sprintf(c, "%05X", aa);

cout<

3、使用stringstream

int aa = 30;

stringstream ss;

ss<

string s1 = ss.str();

cout<

string s2;

ss>>s2;

cout<

可以这样理解,stringstream可以吞下不同的类型,根据s2的类型,然后吐出不同的类型。

4、使用boost库中的lexical_cast

int aa = 30;string s = boost::lexical_cast(aa);cout<

3和4只能转化为10进制的字符串,不能转化为其它进制的字符串。

string转化为int

1、使用strtol(string to long)

string s = "17";

char* end;

int i = static_cast(strtol(s.c_str(),&end,16));

cout<

i = static_cast(strtol(s.c_str(),&end,10));

cout<

2、使用sscanf

int i;

sscanf("17","%D",&i);

cout<

sscanf("17","%X",&i);

cout<

sscanf("0X17","%X",&i);

cout<

3、使用stringstream

string s = "17";

stringstream ss;

ss<

int i;

ss>>i;

cout<

注:stringstream可以吞下任何类型,根据实际需要吐出不同的类型。

4、使用boost库中的lexical_cast

string s = "17";int i = boost::lexical_cast(s);cout<

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值