[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 950
  • Last Modified:

Convert HexaDecimal Strng to Byte Array Using Java

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
speedygonzalez
Asked:
speedygonzalez
  • 2
  • 2
4 Solutions
 
TommySzalapskiCommented:
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
 
TommySzalapskiCommented:
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
 
for_yanCommented:
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
 
for_yanCommented:
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now