輕松

          記述我學(xué)習(xí)java的里程

          常用鏈接

          統(tǒng)計

          積分與排名

          友情鏈接

          最新評論

          java 編碼轉(zhuǎn)換(轉(zhuǎn))

          由于Unicode兼容ASCII(0~255),因此,上面得到的Unicode就是ASCII。 至于轉(zhuǎn)換成二進(jìn)制或其他進(jìn)制,Java API提供了方便函數(shù),你可以查Java的API手冊。

          以字符a的ASCII為例: int i = 'a'; String iBin = Integer.toBinaryString(i);//二進(jìn)制 String iHex = Integer.toHexString(i);//十六進(jìn)制 String iOct = Integer.toOctalString(i);//八進(jìn)制 String iWoKao = Integer.toString(i,3);//三進(jìn)制或任何你想要的35進(jìn)制以下的進(jìn)制 DEC

          [集]java中進(jìn)行二進(jìn)制,八進(jìn)制,十六進(jìn)制,十進(jìn)制間進(jìn)行相互轉(zhuǎn)換       十進(jìn)制轉(zhuǎn)成十六進(jìn)制: Integer.toHexString(int i) 十進(jìn)制轉(zhuǎn)成八進(jìn)制 Integer.toOctalString(int i) 十進(jìn)制轉(zhuǎn)成二進(jìn)制 Integer.toBinaryString(int i) 十六進(jìn)制轉(zhuǎn)成十進(jìn)制 Integer.valueOf("FFFF",16).toString() 八進(jìn)制轉(zhuǎn)成十進(jìn)制 Integer.valueOf("876",8).toString() 二進(jìn)制轉(zhuǎn)十進(jìn)制 Integer.valueOf("0101",2).toString()

          有什么方法可以直接將2,8,16進(jìn)制直接轉(zhuǎn)換為10進(jìn)制的嗎? 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

          進(jìn)制轉(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 輕松 閱讀(2346) 評論(1)  編輯  收藏 所屬分類: JAVA轉(zhuǎn)貼

          評論

          # re: java 編碼轉(zhuǎn)換(轉(zhuǎn)) 2009-11-17 17:43 咖啡妝

          呵呵有用  回復(fù)  更多評論   

          主站蜘蛛池模板: 个旧市| 宝山区| 汨罗市| 肇庆市| 临洮县| 镇江市| 泽州县| 南昌县| 龙游县| 偃师市| 宜兴市| 阿合奇县| 体育| 桂林市| 黎城县| 革吉县| 古田县| 五家渠市| 旬邑县| 宜都市| 囊谦县| 平潭县| 府谷县| 万载县| 镶黄旗| 治县。| 灵台县| 漠河县| 辉县市| 景洪市| 石棉县| 盖州市| 呼和浩特市| 保定市| 昌平区| 平乡县| 新源县| 台南市| 凤阳县| 临猗县| 镇康县|