廣告

2015年1月13日 星期二

[Java]convert data type X into type Y in Java

package test;

public class testDataConversion {

 public static void main(String[] args) {

  //integer to String
  int i = 11; 
  String stri = Integer.toString(i);
  
  //String to integer 
  stri = "25";
  i = Integer.parseInt(stri);
  i = Integer.parseInt(stri);
  
  
  //double to String 
  double d = 1.0;
  String strd = Double.toString(d);
  
  //String to double
  d = Double.valueOf(strd).doubleValue();
  d = Double.parseDouble(strd);
  
  
  //float to String
  float f = 111;
  String strf = Float.toString(f);
  
  //String to float
  f = Float.valueOf(strf).floatValue();
  f = Float.parseFloat(strf);
  
  
  //long to String
  long l =111111;
  String strl = Long.toString(l);
  //String to long
  l = Long.valueOf(strl).longValue();
  l = Long.parseLong(strl);
  
  
  //decimal to binary
  int decimal = 11;
  String binstr = Integer.toBinaryString(decimal);
  
  //decimal to hexadecimal
  decimal = 1111;
  String hexstr = Integer.toString(decimal, 16);
  String hexstr2 = Integer.toHexString(decimal);
  
  //hexadecimal (String) to integer
  decimal  = Integer.valueOf("B8DA34", 16).intValue();
  decimal  = Integer.parseInt("B8DA34", 16);
  
  
  //ASCII code to String
  int ASCII = 64;  
  String aChar = new Character((char)ASCII).toString();
  
  //integer to ASCII code (byte):
  char c = 'A';  
  ASCII = (int) c; 
  
 }

}


沒有留言:

張貼留言