回答
假設你不想使用內置的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);
}
簡單:
String hex = Integer.toHexString(int);
基本上這樣做是創建一個新的字符串,然後調用從名爲toHexString Integer類這就需要一個int ARG的方法。 因此,通過int你想改變成這個方法,你會得到一個字符串與你的int的十六進制版本回來。
您可以將十六進制值放在int類型中,但不能將int類型轉換爲另一個int類型,就我所知,當您正在進行十六進制轉換時。
請記住,您返回的值是一個字符串,因此您無法修改該值,否則您將得到一個數字格式異常。
那麼看看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
如果這是標準的庫實現,那麼我很驚訝他們分配一個數組的四倍大到永遠需要! – mikera 2010-05-17 17:01:38
檢查這
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());
}
}
這種方式失敗的方式有很多種。產生10-15的奇怪值。我懷疑「intToConvert%16」也會用負數做一些奇怪的事情。 – mikera 2010-05-17 16:59:43
@mikera固定的第一點。感謝您的評論。 – 2010-05-18 01:38:24
- 1. 將十六進制(二進制補碼)轉換爲Java值
- 2. 將十進制轉換爲十六進制和十六進制
- 3. 在VB6中將十六進制值轉換爲十進制值
- 4. 十進制轉換爲十六進制的轉換(Java)的
- 5. 將十六進制值轉換爲ascii
- 6. 將十六進制值轉換爲bigint
- 7. 如何將十六進制值轉換爲java中的float值?
- 8. 在最短的代碼中將二進制轉換爲十六進制
- 9. 在Visual C++中將十進制值轉換爲十六進制
- 10. 在Python中將十六進制值轉換爲十進制
- 11. 將十六進制轉換爲二進制到十六進制?
- 12. 將RGBA值轉換爲十六進制顏色代碼
- 13. 將Java代碼轉換爲php - 也許大約十六進制爲二進制
- 14. 如何將ASCII轉換爲java中的十六進制值
- 15. 如何將十六進制轉換爲Java中的ASCII值?
- 16. 將長十六進制代碼轉換爲PHP中的短十六進制代碼
- 17. 將十進制轉換爲十六進制值
- 18. 將十六進制轉換爲十進制值
- 19. 使用遞歸將十六進制轉換爲十進制值
- 20. 將十進制值轉換爲32位浮點十六進制
- 21. 將4字節十六進制轉換爲十進制值
- 22. VB.Net - 將多字節十六進制值轉換爲十進制
- 23. 將十六進制轉換爲十進制值? w/an example
- 24. 此代碼爲什麼要將十六進制轉換爲十進制
- 25. 轉換javascript十六進制代碼
- 26. 轉換十六進制代碼到PHP
- 27. 將字符串轉換爲十六進制到十六進制
- 28. 在Java中將十六進制字符串轉換爲ASCII碼
- 29. 將兩位十六進制代碼轉換爲Unicode編碼
- 30. 將十六進制代碼字符串轉換爲十進制的int []
爲什麼你想要一個優化的解決方案? – monn 2010-05-17 10:26:09
爲什麼不允許使用任何內置函數? – Esko 2010-05-17 10:30:50
您無法將int轉換爲任何基地,它將始終以二進制形式存儲。但是,您可以在其他基地顯示一個int。 – Oak 2010-05-17 10:33:15