2010-04-26 135 views
1

感謝您的回答...我有31 32 2E 30 31 33 6byte十六進制數字。我想將這個6字節的十六進制數字31 32 2E 30 31 33轉換爲java中的12.013 ASCII數字。如何將十六進制轉換爲Java中的ASCII值?

+0

你爲什麼認爲這些數字是相等的?首先看看你的第一個(十六進制)數字比第二個數字大得多。 – Roman 2010-04-26 07:17:25

回答

5

像這樣的事情?

byte[] bytes = {0x31, 0x32, 0x2E, 0x30, 0x31, 0x33}; 
String result = new String(bytes, "ASCII"); 
System.out.println(result); 
0

也許不是最優雅的方法,但試試這個:

char[6] string = new char[6]; 
string[0] = 0x31; 
string[1] = 0x32; 
string[2] = 0x2E; 
string[3] = 0x30; 
string[4] = 0x31; 
string[5] = 0x33; 

String s = new String(string); 

int result = Integer.parseInt(s); 
0

假設你的輸入是代表十六進制數字的字符串數組,你可以做到以下幾點:

public static String convert(String[] hexDigits){ 
    byte[] bytes = new byte[hexDigits.length]; 

    for(int i=0;i<bytes.length;i++) 
     bytes[i] = Integer.decode("0x"+hexDigits[i]).byteValue(); 

    String result; 
    try { 
     result = new String(bytes, "ASCII"); 
    } catch (UnsupportedEncodingException e) { 
     throw new RuntimeException(e); 
    } 
    return result; 
} 

注意,代碼假定數字是給出有效的ASCII值,沒有基數說明符。