在Java中将hexstring转换为ASCII
我希望这不是一个太愚蠢的问题,我已经看了谷歌的5个不同的结果页面,但一直没能find任何东西。
我需要做的是将包含所有hex字符的string转换为ASCII
String fileName = 75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000
我所看到的每一个方式都使得你必须首先把它放到一个数组中。 有没有办法循环通过每两个转换它们?
只需使用for循环遍历string中的每个字符,将它们转换为字符,然后敲击string构build器末尾的字符:
public static void main(String[] args) { String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000"; StringBuilder output = new StringBuilder(); for (int i = 0; i < hex.length(); i+=2) { String str = hex.substring(i, i+2); output.append((char)Integer.parseInt(str, 16)); } System.out.println(output); }
这给出了几行从下面开始:
uTorrent \ Completed \ nfsuc_ost_by_mustang \ Pendulum-9,000 Miles.mp3
嗯… 🙂
检出使用Java将hex转储的string表示forms转换为字节数组?
忽略编码等,你可以做new String (hexStringToByteArray("75546..."));
所以,据我了解,你需要拉出连续的hex数字对,然后解码这个2位hex数字,并采取相应的字符:
String s = "..."; StringBuilder sb = new StringBuilder(s.length() / 2); for (int i = 0; i < s.length(); i+=2) { String hex = "" + s.charAt(i) + s.charAt(i+1); int ival = Integer.parseInt(hex, 16); sb.append((char) ival); } String string = sb.toString();
String hexToAscii(String s) { int n = s.length(); StringBuilder sb = new StringBuilder(n / 2); for (int i = 0; i < n; i += 2) { char a = s.charAt(i); char b = s.charAt(i + 1); sb.append((char) ((hexToInt(a) << 4) | hexToInt(b))); } return sb.toString(); } private static int hexToInt(char ch) { if ('a' <= ch && ch <= 'f') { return ch - 'a' + 10; } if ('A' <= ch && ch <= 'F') { return ch - 'A' + 10; } if ('0' <= ch && ch <= '9') { return ch - '0'; } throw new IllegalArgumentException(String.valueOf(ch)); }
//%%%%%%%%%%%%%%%%%%%%%% HEX to ASCII %%%%%%%%%%%%%%%%%%%%%% public String convertHexToString(String hex){ String ascii=""; String str; // Convert hex string to "even" length int rmd,length; length=hex.length(); rmd =length % 2; if(rmd==1) hex = "0"+hex; // split into two characters for( int i=0; i<hex.length()-1; i+=2 ){ //split the hex into pairs String pair = hex.substring(i, (i + 2)); //convert hex to decimal int dec = Integer.parseInt(pair, 16); str=CheckCode(dec); ascii=ascii+" "+str; } return ascii; } public String CheckCode(int dec){ String str; //convert the decimal to character str = Character.toString((char) dec); if(dec<32 || dec>126 && dec<161) str="n/a"; return str; }
使用javax.xml.bind.DatatypeConverter
最简单的方法:
String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000"; byte[] s = DatatypeConverter.parseHexBinary(hex); System.out.println(new String(s));
对于这种情况下,我有一个hex数据格式到一个int数组,我想转换它们在string。
int[] encodeHex = new int[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f }; // Hello encode for (int i = 0; i < encodeHex.length; i++) { System.out.print((char) (encodeHex[i])); }