2014-10-09 189 views
1

HI我必須嘗試多種方法才能將Hex String轉換爲ASCII String,但不能獲得成功。雖然之前我做了同樣的事情,但現在我無法實現它。將十六進制字符串轉換爲字符串

我的代碼是

private static String hexToASCII(String hexValue) 
{ 
    StringBuilder output = new StringBuilder(""); 
    for (int i = 0; i < hexValue.length(); i += 2) 
    { 
     String str = hexValue.substring(i, i + 2); 
     output.append((char) Integer.parseInt(str, 16)); 
    } 
    return output.toString(); 
} 

但像b��¡

返回垃圾的價值和我的十六進制字符串是

621c8002008a820101a10a8c0341c2009c0341c2008302010288008a0105 

請幫我,如果有人也從同一個問題遭遇並修復它。

感謝....

+1

http://stackoverflow.com/questions/4785654/convert-a-string-of-hex-into-ascii-in- java – Cativail 2014-10-09 10:24:52

+0

Colud發送你在參數 – Palak 2014-10-09 10:25:27

+0

中傳遞的「hexValue」的值在創建StringBuilder的* Object *的時候刪除'「」'檢查'hexValue'中是否有正確的值 – kId 2014-10-09 10:26:33

回答

1

嘗試了這一點

public class HextoAsscii { 

    public static void main(String args[]) 
    { 
     String hex="621c8002008a820101a10a8c0341c2009c0341c2008302010288008a0105"; 
     String str=""; 
     str= hexToASCII(hex); 

    } 
    private static String hexToASCII(String hexValue) 
    { 
     StringBuilder output = new StringBuilder(""); 

     for (int i = 0; i < hexValue.length(); i += 2) 
     { 
      if(i+2<=hexValue.length()) 
      { 
      String str = hexValue.substring(i, i + 2); 
      output.append(Integer.parseInt(str, 16)); 
      } 
     } 
     System.out.println(output.toString()); 
     return output.toString(); 
    } 
} 
+0

嘿它是返回響應爲「982812820138130111611014036519401563651940131212136013815」,而我想字符串 – 2014-10-09 10:50:25

+0

是的,它是整數的Ascii值,我已經從output.append((char)Integer.parseInt(str,16))刪除了char。 http://tomeko.net/online_tools/hex_to_ascii.php?lang=en- hex to ascii ...儘管字符存在,但您得到的是正確的Ascii,但您得到了一些特殊符號,即使這些特殊符號代表了ascii代碼 - 點擊下面的鏈接以獲得想法 - http://www.asciitable.com/ - 請參閱擴展ascii代碼。 – 2014-10-09 10:53:20

相關問題