java 編碼轉(zhuǎn)換(轉(zhuǎn))
由于Unicode兼容ASCII(0~255),因此,上面得到的Unicode就是ASCII。 至于轉(zhuǎn)換成二進制或其他進制,Java API提供了方便函數(shù),你可以查Java的API手冊。
以字符a的ASCII為例: int i = 'a'; String iBin = Integer.toBinaryString(i);//二進制 String iHex = Integer.toHexString(i);//十六進制 String iOct = Integer.toOctalString(i);//八進制 String iWoKao = Integer.toString(i,3);//三進制或任何你想要的35進制以下的進制 DEC
[集]java中進行二進制,八進制,十六進制,十進制間進行相互轉(zhuǎn)換 十進制轉(zhuǎn)成十六進制: Integer.toHexString(int i) 十進制轉(zhuǎn)成八進制 Integer.toOctalString(int i) 十進制轉(zhuǎn)成二進制 Integer.toBinaryString(int i) 十六進制轉(zhuǎn)成十進制 Integer.valueOf("FFFF",16).toString() 八進制轉(zhuǎn)成十進制 Integer.valueOf("876",8).toString() 二進制轉(zhuǎn)十進制 Integer.valueOf("0101",2).toString()
有什么方法可以直接將2,8,16進制直接轉(zhuǎn)換為10進制的嗎? java.lang.Integer類 parseInt(String s, int radix) 使用第二個參數(shù)指定的基數(shù),將字符串參數(shù)解析為有符號的整數(shù)。 examples from jdk: parseInt("0", 10) returns 0 parseInt("473", 10) returns 473 parseInt("-0", 10) returns 0 parseInt("-FF", 16) returns -255 parseInt("1100110", 2) returns 102 parseInt("2147483647", 10) returns 2147483647 parseInt("-2147483648", 10) returns -2147483648 parseInt("2147483648", 10) throws a NumberFormatException parseInt("99", 8) throws a NumberFormatException parseInt("Kona", 10) throws a NumberFormatException parseInt("Kona", 27) returns 411787
進制轉(zhuǎn)換如何寫(二,八,十六)不用算法 Integer.toBinaryString Integer.toOctalString Integer.toHexString
例一:
public class Test{ public static void main(String args[]){
int i=100; String binStr=Integer.toBinaryString(i); String otcStr=Integer.toOctalString(i); String hexStr=Integer.toHexString(i); System.out.println(binStr);
例二:
public class TestStringFormat { public static void main(String[] args) { if (args.length == 0) { System.out.println("usage: java TestStringFormat <a number>"); System.exit(0); }
Integer factor = Integer.valueOf(args[0]);
String s;
s = String.format("%d", factor); System.out.println(s); s = String.format("%x", factor); System.out.println(s); s = String.format("%o", factor); System.out.println(s); } }
各種數(shù)字類型轉(zhuǎn)換成字符串型:
String s = String.valueOf( value); // 其中 value 為任意一種數(shù)字類型。
字符串型轉(zhuǎn)換成各種數(shù)字類型:
String s = "169"; byte b = Byte.parseByte( s ); short t = Short.parseShort( s ); int i = Integer.parseInt( s ); long l = Long.parseLong( s ); Float f = Float.parseFloat( s ); Double d = Double.parseDouble( s );
數(shù)字類型與數(shù)字類對象之間的轉(zhuǎn)換:
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; 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();
posted on 2009-11-17 13:57 輕松 閱讀(2345) 評論(1) 編輯 收藏 所屬分類: JAVA轉(zhuǎn)貼