Solved

Convert HexaDecimal Strng to Byte Array Using Java

Posted on 2011-02-22
5
861 Views
Last Modified: 2012-05-11
Hello,

I'm trying to find out how to convert a hexadecimal string to a byte array using Java.

For example if I want to display a value such as d079061eff52d88e8ef80db06461466e as "-38, 111, 6, 31, 91, 81, -40, -114, -114, -40, 13, -80, 116, 97, 72, 110" then what do I do. (The above is a made up hex string just for representation here)

I've tried to do this using a class as per the code snippet I've attached here But it prints out something similar to "Ðy\RØŽŽØ
°taHn"

That is not what I want though as I want the numbered format similar to the numbers 38, 111, 6, 31,  etc above.


Any ideas/help on this?

Thanks!
public class CipherTextByteConverter {
	
	public CipherTextByteConverter(){
		
	}
	
    public static void main(String[] args) {

    	CipherTextByteConverter cipherConvert=new CipherTextByteConverter();
    	
        String hexVal = "d079061eff52d88e8ef80db06461466e";
       String value = new String(cipherConvert.hexToBytes(hexVal));
        
        System.out.println(value);

    }	

    public static byte[] hexToBytes(String hexVal) {
        int len = hexVal.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
          data[i / 2] = (byte) ((Character.digit(hexVal.charAt(i), 16) << 4)
      	                             + Character.digit(hexVal.charAt(i+1), 16));
        }
        
        return data;
    }	    
}

Open in new window

0
Comment
Question by:speedygonzalez
  • 2
  • 2
5 Comments
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 250 total points
ID: 34953536
Convert each byte to an integer not a string. Then display. The problem is that it's interpreting the byte as a character. You want it to think it's an integer.
Something like:
    public static string hexToBytes(String hexVal) {
        int len = hexVal.length();
        byte datum
        string str = ""];
        for (int i = 0; i < len; i += 2) {
          datum = (byte) ((Character.digit(hexVal.charAt(i), 16) << 4)
      	                             + Character.digit(hexVal.charAt(i+1), 16));
        str = str + ", " + String( (int)datum)
        }
        
        return data;
    }	

Open in new window

0
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 250 total points
ID: 34953542
The syntax might not be perfect, but you should be able to get it from there.
Oh, and change it to
return str;
of course
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 250 total points
ID: 34953567
You just print it differently:
    public class CipherTextByteConverter {

	public CipherTextByteConverter(){

	}

    public static void main(String[] args) {

    	CipherTextByteConverter cipherConvert=new CipherTextByteConverter();

        String hexVal = "d079061eff52d88e8ef80db06461466e";
       String value = new String(cipherConvert.hexToBytes(hexVal));

        byte [] bytes =    cipherConvert.hexToBytes(hexVal);
       for(int j=0;j<bytes.length; j++){
           System.out.print("" + bytes[j]);
       }
        //System.out.println(value);

    }

    public static byte[] hexToBytes(String hexVal) {
        int len = hexVal.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
          data[i / 2] = (byte) ((Character.digit(hexVal.charAt(i), 16) << 4)
      	                             + Character.digit(hexVal.charAt(i+1), 16));
        }

        return data;
    }
}

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 250 total points
ID: 34953613
Added commas to separate:

    public class CipherTextByteConverter {

	public CipherTextByteConverter(){

	}

    public static void main(String[] args) {

    	CipherTextByteConverter cipherConvert=new CipherTextByteConverter();

        String hexVal = "d079061eff52d88e8ef80db06461466e";
       String value = new String(cipherConvert.hexToBytes(hexVal));

        byte [] bytes =    cipherConvert.hexToBytes(hexVal);
       for(int j=0;j<bytes.length; j++){
           System.out.print("" + bytes[j] +",");
       }
        //System.out.println(value);

    }

    public static byte[] hexToBytes(String hexVal) {
        int len = hexVal.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
          data[i / 2] = (byte) ((Character.digit(hexVal.charAt(i), 16) << 4)
      	                             + Character.digit(hexVal.charAt(i+1), 16));
        }

        return data;
    }
}

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 34956064
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now