posts - 25,comments - 0,trackbacks - 0
          十進(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", 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); 
             } 




          其他方法: 

          Integer.toHexString(你的10進(jìn)制數(shù)); 
          例如 
          String temp = Integer.toHexString(75); 
          輸出temp就為 4b 



          //輸入一個10進(jìn)制數(shù)字并把它轉(zhuǎn)換成16進(jìn)制 
          import java.io.*; 
          public class toHex{ 

          public static void main(String[]args){ 

          int input;//存放輸入數(shù)據(jù) 
          //創(chuàng)建輸入字符串的實例 
          BufferedReader strin=new BufferedReader(new InputStreamReader(System.in)); 
          System.out.println("請輸入一個的整數(shù):"); 
          String x=null; 
          try{ 
          x=strin.readLine(); 
          }catch(IOException ex){ 
          ex.printStackTrace(); 

          input=Integer.parseInt(x); 
          System.out.println ("你輸入的數(shù)字是:"+input);//輸出從鍵盤接收到的數(shù)字 

          System.out.println ("它的16進(jìn)制是:"+Integer.toHexString(input));//用toHexString把10進(jìn)制轉(zhuǎn)換成16進(jìn)制 
          posted on 2012-04-29 12:10 周磊 閱讀(3173) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 来宾市| 石狮市| 乌兰浩特市| 蚌埠市| 齐河县| 古丈县| 澎湖县| 云梦县| 台湾省| 蚌埠市| 曲阜市| 禄丰县| 洪洞县| 潜江市| 赤城县| 江阴市| 龙江县| 郧西县| 瑞昌市| 浙江省| 漠河县| 措勤县| 滨州市| 新民市| 图木舒克市| 绥棱县| 嘉黎县| 汕头市| 南宫市| 沁阳市| 瓦房店市| 阿瓦提县| 柘城县| 元谋县| 葫芦岛市| 东山县| 嵩明县| 故城县| 友谊县| 灌云县| 阳原县|