1 字符串轉換成數據
字符串轉換成整數:
String MyNumber ="1234";
int MyInt = Integer.parseInt(MyNumber);
字符串轉換成byte,short, int, float, double, long等數據類型,可以分別參考Byte, Short,Integer, Float, Double, Long類的parseXXX方法。
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 MyInt = 1234;
String MyString = "" + MyInt;
其它數據類型可以利用同樣的方法轉換成字符串。
1.)String s = String.valueOf(i);
2.)String s = Integer.toString(i);
3.)String s = "" + i;
注:Double, Float, Long 轉成字串的方法大同小異.
3 十進制到其他進制的轉換
十進制整數轉換成二進制整數,返回結果是一個字符串:
Integer.toBinaryString(int i);
Integer和Long提供了toBinaryString, toHexString和toOctalString方法,可以方便的將數據轉換成二進制、十六進制和八進制字符串。功能更加強大的是其toString(int/longi, int radix)方法,可以將一個十進制數轉換成任意進制的字符串形式。
byte, short, float和double等數據類型,可以利用Integer或者是Long的toBinaryString,toHexString, to OctalString和toString方法轉換成其他進制的字符串形式。
4 其它進制到十進制的轉換
五進制字符串14414轉換成十進制整數,結果是1234:
System.out.println(Integer.valueOf("14414", 5);
Integer和Long提供的valueOf(String source, int radix)方法,可以將任意進制的字符串轉換成十進制數據。
5 整數到字節數組的轉換
public static byte[] toByteArray(int number)
{
int temp = number;
byte[] b=new byte[4];
for (int i = b.length - 1; i > -1; i--)
{
b[i] = new Integer(temp & 0xff).byteValue();
temp = temp >> 8;
}
return b;
}
6 字節數組到整數的轉換
public static int toInteger(byte[] b)
{
int s = 0;
for (int i = 0; i < 3; i++)
{
if (b[i] > 0)
s = s + b[i];
else
s = s + 256 + b[i];
s = s * 256;
}
if (b[3] > 0)
s = s + b[3];
else
s = s + 256 + b[3];
return s;
}
7 短整數與字節數組之間的相互轉換
short與int之間的區別在於short是兩個字節的,而int是四個字節的。因此,只需要將5 與6 中的範例程序小做改動,即可實現短整數與字節數組之間的相互轉換。
8 字節數組轉換成雙精度浮點數
public double toDouble(byte[] b)
{
long l = 0;
Double D = new Double(0.0);
l = b[0];
l |= ((long)b[1]<<8);
l |= ((long)b[2]<<16);
l |= ((long)b[3]<<24);
l |= ((long)b[4]<<32);
l |= ((long)b[5]<<40);
l |= ((long)b[6]<<48);
l |= ((long)b[7]<<56);
return D.longBitsToDouble(l);
}
9 布爾類型轉換成字符串
第一種方法是:
boolean bool = true;
String s = new Boolean(bool).toString();//將bool利用對象封裝器轉化爲對象
s.equals("true");
/* 其中,toString方法是一個繼承方法。java中所有的類都是object的繼承,object的一個重要方法就是toString,用於將對象轉化爲字符串。*/
第二種方法是:
boolean bool = true;
String s = String.valueOf( bool );
首先,從代碼長度上講第二種方法明顯要比第一種方法簡潔;其次,第一種方法在轉化過程中多引入了一個完全沒有必要的對象,因此,相對第二種方法來說這就造成了內存空間的浪費,大大減慢了運行速度。所以,推薦使用第二種方法。
10 數字類型與數字類對象之間的轉換
byte b = 169;
Byte bo = new Byte( b );
b = bo.byteValue();
short t = 169;
Short to = new Short( t );
t = to.shortValue();
int i = 169;
Integer io = new Integer( i ); //轉換爲整型
i = io.intValue();
long l = 169;
Long lo = new Long( l );
l = lo.longValue();
float f = 169f;
Float fo = new Float( f );
f = fo.floatValue();
double d = 169f;
Double dObj = new Double( d );
d = dObj.doubleValue();
*****************************************************************************************************
這是一個例子,說的是JAVA中數據數型的轉換.供大家學習引
packagecn.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中常用數據類型轉換函數
雖然都能在JAVAAPI中找到,整理一下做個備份。
string->byte
Byte static byte parseByte(String s)
byte->string
Byte static String toString(byte b)
char->string
Character static String to String (char c)
string->Short
Short static Short parseShort(String s)
Short->String
Short static String toString(Short s)
String->Integer
Integer static int parseInt(String s)
Integer->String
Integer static String tostring(int i)
String->Long
Long static long parseLong(String s)
Long->String
Long static String toString(Long i)
String->Float
Float static float parseFloat(String s)
Float->String
Float static String toString(float f)
String->Double
Double static double parseDouble(String s)
Double->String
Double static String toString(Double
**************************************************************************************************
string->byte
Byte static byte parseByte(String s)
byte->string
Byte static String toString(byte b)
char->string
Character static String to String (char c)
string->Short
Short static Short parseShort(String s)
Short->String
Short static String toString(Short s)
String->Integer
Integer static int parseInt(String s)
Integer->String
Integer static String tostring(int i)
String->Long
Long static long parseLong(String s)
Long->String
Long static String toString(Long i)
String->Float
Float static float parseFloat(String s)
Float->String
Float static String toString(float f)
String->Double
Double static double parseDouble(String s)
Double->String
Double static String toString(Double d)
date=java.sql.Date.valueOf(String s);