Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a String of Hex into ASCII in Java

I hope this isn't too much of a stupid question, I have looked on 5 different pages of Google results but haven't been able to find anything on this.

What I need to do is convert a string that contains all Hex characters into ASCII for example

String fileName =   75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000 

Every way I have seen makes it seems like you have to put it into an array first. Is there no way to loop through each two and convert them?

like image 305
James Avatar asked Jan 24 '11 18:01

James


People also ask

How do I encode a Java string as ASCII?

Convert String to ASCII 2.1 We can use String. getBytes(StandardCharsets. US_ASCII) to convert the string into a byte arrays byte[] , and upcast the byte to int to get the ASCII value. 2.2 Java 9, there is a new API String.

What is hex string in Java?

Hex String – A Hex String is a combination of the digits 0-9 and characters A-F, just like how a binary string comprises only 0's and 1's. Eg: “245FC” is a hexadecimal string. Byte Array – A Java Byte Array is an array used to store byte data types only. The default value of each element of the byte array is 0.

What is ASCII hex format?

ASCII stands for American Standard Code for Information Interchange. It ranges from 0 to 255 in Decimal or 00 to FF in Hexadecimal. ASCII codes can be divided into two sets - Standard ASCII codes and Extended ASCII codes.

Which function is used to convert hexadecimal values to the ASCII characters *?

sprintf() will convert your presumably binary data to an ASCII hex string or a decimal hex string.


2 Answers

Just use a for loop to go through each couple of characters in the string, convert them to a character and then whack the character on the end of a string builder:

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); 

Or (Java 8+) if you're feeling particularly uncouth, use the infamous "fixed width string split" hack to enable you to do a one-liner with streams instead:

System.out.println(Arrays         .stream(hex.split("(?<=\\G..)")) //https://stackoverflow.com/questions/2297347/splitting-a-string-at-every-n-th-character         .map(s -> Character.toString((char)Integer.parseInt(s, 16)))         .collect(Collectors.joining())); 

Either way, this gives a few lines starting with the following:

uTorrent\Completed\nfsuc_ost_by_mustang\Pendulum-9,000 Miles.mp3

Hmmm... :-)

like image 83
Michael Berry Avatar answered Oct 06 '22 01:10

Michael Berry


Easiest way to do it with javax.xml.bind.DatatypeConverter:

    String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";     byte[] s = DatatypeConverter.parseHexBinary(hex);     System.out.println(new String(s)); 
like image 31
Nikita Koksharov Avatar answered Oct 06 '22 02:10

Nikita Koksharov