2010-05-17 43 views
0

我需要將整數值轉換爲十六進制。Java中將Integer值轉換爲十六進制的最佳代碼

我已經完成了一些邏輯,但我想要優化的解決方案。

編輯:對不起,我忘了發佈,我不允許使用任何內置功能。

+1

爲什麼你想要一個優化的解決方案? – monn 2010-05-17 10:26:09

+1

爲什麼不允許使用任何內置函數? – Esko 2010-05-17 10:30:50

+1

您無法將int轉換爲任何基地,它將始終以二進制形式存儲。但是,您可以在其他基地顯示一個int。 – Oak 2010-05-17 10:33:15

回答

4

假設你不想使用內置的toHexString出於某種原因,這裏是一個非常有效的方式來做到這一點:

public static char toHexChar(int i) { 
    i&=15; 
    return (i<10)? (char)(i+48) : (char)(i+55); 
} 

public static String toHexString(int n) { 
    char[] chars=new char[8]; 
    for (int i=0; i<8; i++) { 
    chars[7-i]=toHexChar(n); 
    n>>=4; 
    }; 
    return new String(chars); 
} 
+1

這會打印前導零,對不對?像'00000005'輸入'5'? – aioobe 2010-05-17 10:43:16

+0

注意這個實現總是以八個十六進制數字的形式返回一個答案......在大多數情況下,這可能是你想要的,但是如果你想完全匹配內置的Java函數,你需要刪除前導零。 – mikera 2010-05-17 10:45:59

+0

@aioobe是正確的。品味我想,但我總是喜歡保持領先的零,提醒我我正在處理32位二進制數量 – mikera 2010-05-17 10:47:26

5

簡單:

String hex = Integer.toHexString(int); 

基本上這樣做是創建一個新的字符串,然後調用從名爲toHexString Integer類這就需要一個int ARG的方法。 因此,通過int你想改變成這個方法,你會得到一個字符串與你的int的十六進制版本回來。

您可以將十六進制值放在int類型中,但不能將int類型轉換爲另一個int類型,就我所知,當您正在進行十六進制轉換時。

請記住,您返回的值是一個字符串,因此您無法修改該值,否則您將得到一個數字格式異常。

4

那麼看看Integer.toHexString(int)的執行情況。以下代碼是從java標準庫中的Integer類中提取的。

public class Test { 

    final static char[] digits = { 
     '0' , '1' , '2' , '3' , '4' , '5' , 
     '6' , '7' , '8' , '9' , 'a' , 'b' , 
     'c' , 'd' , 'e' , 'f' 
    }; 

    private static String intAsHex(int i) { 
     char[] buf = new char[32]; 
     int charPos = 32; 
     int radix = 1 << 4; 
     int mask = radix - 1; 
     do { 
      buf[--charPos] = digits[i & mask]; 
      i >>>= 4; 
     } while (i != 0); 

     return new String(buf, charPos, (32 - charPos)); 
    } 


    public static void main(String... args) { 
     System.out.println(intAsHex(77)); 
    } 
} 

輸出:4d

+0

如果這是標準的庫實現,那麼我很驚訝他們分配一個數組的四倍大到永遠需要! – mikera 2010-05-17 17:01:38

1

檢查這

public class IntToHexa { 
    public static void main(java.lang.String args[]){ 
     /* 
     * Here we need an integer to convert. 
     * [1]You can pass as command line argument 
     * [2]You can get as input from console 
     * [3]Take a constant. Here I'm taking a constant 
     */ 
     int intToConvert = 450; 
     java.lang.StringBuilder convertedHexa = new java.lang.StringBuilder(""); 
     while (intToConvert > 15){ 
      /* 
     * If the reminder is less than 10, add the remainder. else get the equivalent hexa code 
     * Here I'm getting the character code and adding the charater to the hexa string. 
     * For that I'm getting the difference between the reminder and 10. 
     * For example, if the reminder is 13, the reminder will be 3. 
     * Then add that difference to 65. In this example, it will become 68. 
     * Finally, get the quivalent char code of the result number. Here it will be D. 
     * Same for number, I'm adding it to 48 
     */ 
      convertedHexa.append(intToConvert % 16 < 10 ? ((char)(48 + (intToConvert % 16))) : ((char)(65 + (intToConvert % 16 - 10)))); 
      intToConvert /= 16; 
     } 
     convertedHexa.append(intToConvert % 16 < 10 ? ((char)(48 + (intToConvert % 16))) : ((char)(65 + (intToConvert % 16 - 10)))); 
     java.lang.System.out.println(convertedHexa.reverse()); 
    } 
} 
+1

這種方式失敗的方式有很多種。產生10-15的奇怪值。我懷疑「intToConvert%16」也會用負數做一些奇怪的事情。 – mikera 2010-05-17 16:59:43

+0

@mikera固定的第一點。感謝您的評論。 – 2010-05-18 01:38:24

相關問題