java中数据类型之间的的转换
/**
* @author Sunky
* Create on 2005-5-17
*
*
*/
public class Test0517 {
public static void main(String[] args) {
int i = 0;
/**********************************************
* Convert from type String to type Integer *
**********************************************/
String str = "25";
String drt = "35.214";
long l = 8900;
float f = 98945632;
//Method NO.1
//i = Integer.parseInt(str);
//Method NO.2
i = Integer.valueOf(str).intValue();
System.out.println("Conver String to Integer i: " + i );
/**
* Convert from type Integer to type String
*/
String ItoS = Integer.toString(i);
System.out.println("Conver Integer to String ItoS: " + ItoS);
/***
* Convert from type String to type double
*/
double d = Double.valueOf(drt).doubleValue();
System.out.println("Convert from type String to type double d : " + d );
/**
* Convert from type double to type String
*/
String DtoS = Double.toString(d);
System.out.println("Convert from type Double to type String DtoS : " + DtoS );
/**
* Convert from type long to type String
*/
String LtoS = Long.toString(l);
System.out.println("Convert from type Double to type String LtoS : " + LtoS );
/**
* Convert from type float to type String
*/
String FtoS = Float.toString(f);
System.out.println("Convert from type Float to type String FtoS : " + FtoS );
/**
* Convert from type String to type Long
*/
long StoL1 = Long.valueOf(str).longValue();
System.out.println("Convert from type String to type Long StoL1 : " + StoL1 );
long StoL2= Long.parseLong(str);
System.out.println("Convert from type String to type Long StoL2 : " + StoL2 );
/**
* Convert from type String to type float
*/
float StoF= Float.valueOf(str).floatValue();
System.out.println("Convert from type String to type float StoF : " + StoF );
try{
/**
* Convert from type decimal to type binary
*/
int n = 42;
String binstr = Integer.toBinaryString(n);
System.out.println("Convert from type String to type binary binstr : " + binstr );
/**
* Convert from type decimal to type hexadecimal
*/
String hexstr = Integer.toString(n, 16);
System.out.println("Convert from type String to type hexadecimal hexstr : " + hexstr );
System.out.print
(Integer.toHexString( 0x10000 | n).substring(1).toUpperCase());
/**
* hexadecimal (String) to integer
*/
int hex = Integer.valueOf("B8DA3", 16).intValue();
System.out.println("Convert from type hexadecimal to type String hex : " + hex );
int hex2 = Integer.parseInt("B8DA3", 16);
System.out.println("Convert from type hexadecimal to type String hex2 : " + hex2 );
/**
* To extract Ascii codes from a String
*/
String test = "SKY";
for ( int num = 0; num < test.length(); ++num ) {
char c = test.charAt( num );
int j = (int) c;
System.out.print(j);
System.out.println("");
}
}catch(Exception e){
e.printStackTrace();
}
}
}