Solved

Java Hex to String

Posted on 2007-11-19
6
18,178 Views
Last Modified: 2013-12-22
Hello

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

41424344

Example output:
ABCD

Thanks from now!
0
Comment
Question by:CSecurity
6 Comments
 
LVL 26

Expert Comment

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

Author Comment

by:CSecurity
ID: 20311859
Hex maximum length is 2
0
 
LVL 17

Author Comment

by:CSecurity
ID: 20311861
and for hex with length 1 there will be zero before it
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 13

Accepted Solution

by:
Bart Cremers earned 500 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();
    }

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20311970

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

Open in new window

0
 
LVL 86

Expert Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question