輕松

          記述我學習java的里程

          常用鏈接

          統(tǒng)計

          積分與排名

          友情鏈接

          最新評論

          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)貼

          評論

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

          呵呵有用  回復  更多評論   

          主站蜘蛛池模板: 双鸭山市| 鹤壁市| 收藏| 龙海市| 灵丘县| 大英县| 临桂县| 康定县| 济南市| 邵武市| 黄大仙区| 武功县| 枣庄市| 织金县| 潜江市| 双鸭山市| 天镇县| 长岛县| 温宿县| 延津县| 永登县| 凌海市| 上蔡县| 哈巴河县| 靖安县| 汨罗市| 永宁县| 九台市| 襄城县| 灵川县| 益阳市| 始兴县| 昆明市| 韶关市| 博白县| 东阳市| 武邑县| 建水县| 日照市| 株洲县| 建平县|