Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# Java Hex to String

Posted on 2007-11-19
Medium Priority
18,216 Views
Hello

I want to convert a Hex String to String.
Example input:

41424344

Example output:
ABCD

Thanks from now!
0
Question by:CSecurity

LVL 26

Expert Comment

ID: 20311849
I don't think you can do that! there should be some delemeter between each char representation...
0

LVL 17

Author Comment

ID: 20311859
Hex maximum length is 2
0

LVL 17

Author Comment

ID: 20311861
and for hex with length 1 there will be zero before it
0

LVL 13

Accepted Solution

Bart Cremers earned 2000 total points
ID: 20311909
Here's a method for use with fixed lengths. Usage:

hexToString("41424344", 2)
``````    public static String hexToString(String input, int groupLength) {
StringBuilder sb = new StringBuilder(input.length() / groupLength);

for (int i = 0; i < input.length() - groupLength + 1; i += groupLength) {
String hex = input.substring(i, i + groupLength);
sb.append((char) Integer.parseInt(hex, 16));
}

return sb.toString();
}
``````
0

LVL 86

Expert Comment

ID: 20311970

``````String s = new String(new BigInteger("41424344").toByteArray()));
``````
0

LVL 86

Expert Comment

ID: 20311987
String s = new String(new BigInteger("41424344", 16).toByteArray()));
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a clâ€¦
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different dâ€¦
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
###### Suggested Courses
Course of the Month11 days, 4 hours left to enroll