Here's a short way to convert it to hex:
public String toHex(String arg) {
return String.format("%040x", new BigInteger(1, arg.getBytes(/*YOUR_CHARSET?*/)));
}
Answer from Kaleb Pederson on Stack OverflowHere's a short way to convert it to hex:
public String toHex(String arg) {
return String.format("%040x", new BigInteger(1, arg.getBytes(/*YOUR_CHARSET?*/)));
}
To ensure that the hex is always 40 characters long, the BigInteger has to be positive:
public String toHex(String arg) {
return String.format("%x", new BigInteger(1, arg.getBytes(/*YOUR_CHARSET?*/)));
}
How can I convert a string into hex in Java - Stack Overflow
Java String to hex string
How to convert hex string to Java string? - Stack Overflow
java - string to hex value - Stack Overflow
You can try like this(Kaleb Pederson answers this):-
public String toHex(String arg) {
return String.format("%040x", new BigInteger(1, arg.getBytes(/*YOUR_CHARSET?*/)));
}
or try this:-
String s= "000102030405060708090A0B0C0D0E0F";
byte[] b= Hex.decodeHex(s.toCharArray());
System.out.println(new String(b, "UTF8"));
The issue here is that you're trying to create a String from binary data, and the default encoding used by the String class is changing the value of that binary.
Example:
String output = new String(new byte[]{(byte)0x90, (byte)0x81, (byte)0x8d, (byte)0x8f, (byte)0x90, (byte)0x9d});
System.out.println(DatatypeConverter.printHexBinary(output.getBytes()));
Output:
3F3F3F3F3F3F
Hang on to the byte[] that parseHexBinary outputs and set a breakpoint so you can examine it's contents directly, you'll notice that it contains the correct binary values.
Update per comments:
If you want to write the output of parseHexBinary to a file, you should write the raw bytes to the file, not a String of the bytes.
DataOutputStream os = new DataOutputStream(new FileOutputStream("someFileName"));
byte[] bytes = DatatypeConverter.parseHexBinary(input);
os.write(bytes, 0, bytes.length);
os.close();
I'm trying to create a genetic algorithm and I've got this code:
public Cromosoma() {
this.setRepresentacionBinaria(this.generarAleatoriamenteCromosoma());
}
private String generarAleatoriamenteCromosoma() {
String cromosoma = "";
for (int i = 1; i <= 40; i++) {
cromosoma += ((int) (Math.random() * 10)) % 2;
}
return cromosoma;
}
public String getRepresentacionBinaria() {
return this.representacionBinaria;
}
public String getRepresentacionHexadecimal() {
return Long.toHexString(Long.parseLong(this.getRepresentacionBinaria(), 2));
}getRepresentacionBinaria() generates a random string of 40 characters(0s and 1s), I need that string to be a Hexadecimal string(without the x), this code does it, the problem comes when Long.parseLong() parses the string and returns a hexadecimal number with leading zeros(which I need) and gives me this error:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 9
Using Hex in Apache Commons:
String hexString = "fd00000aa8660b5b010006acdc0100000101000100010000";
byte[] bytes = Hex.decodeHex(hexString.toCharArray());
System.out.println(new String(bytes, "UTF-8"));
byte[] bytes = javax.xml.bind.DatatypeConverter.parseHexBinary(hexString);
String result= new String(bytes, encoding);