Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert hex string to Java string?

Tags:

java

string

hex

For logging purpose we are converting the logs to byte array and then to hex string. I want to get it back in a Java String, but I am not able to do so.

The hex string in log file looks something like

fd00000aa8660b5b010006acdc0100000101000100010000 

How can I decode this?

like image 342
Samra Avatar asked Dec 21 '12 13:12

Samra


People also ask

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.

How do you pass a hexadecimal value in java?

toHexString and Integer. parseInt(hex, 16) to convert the String to Hex and vice versa. The idea is convert String <==> Decimal <==> Hex , for example char a , decimal is 97, hex is 61.


1 Answers

Using Hex in Apache Commons:

String hexString = "fd00000aa8660b5b010006acdc0100000101000100010000";     byte[] bytes = Hex.decodeHex(hexString.toCharArray()); System.out.println(new String(bytes, "UTF-8")); 
like image 192
Reimeus Avatar answered Oct 02 '22 12:10

Reimeus