How to Convert Hex String to Java String

Convert hex to original string value

Please try this one

String hex = "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000009796f6f6f6f6f6f6f6f0000000000000000000000000000000000000000000000";
StringBuilder output = new StringBuilder();
for (int i = 0; i < hex.length(); i+=2) {
String str = hex.substring(i, i+2);
output.append((char)Integer.parseInt(str, 16));
}
System.out.println(output.toString().trim());

Convert Hexadecimal to String

You can reconstruct bytes[] from the converted string,
here's one way to do it:

public String fromHex(String hex) throws UnsupportedEncodingException {
hex = hex.replaceAll("^(00)+", "");
byte[] bytes = new byte[hex.length() / 2];
for (int i = 0; i < hex.length(); i += 2) {
bytes[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16));
}
return new String(bytes);
}

Another way is using DatatypeConverter, from javax.xml.bind package:

public String fromHex(String hex) throws UnsupportedEncodingException {
hex = hex.replaceAll("^(00)+", "");
byte[] bytes = DatatypeConverter.parseHexBinary(hex);
return new String(bytes, "UTF-8");
}

Unit tests to verify:

@Test
public void test() throws UnsupportedEncodingException {
String[] samples = {
"hello",
"all your base now belongs to us, welcome our machine overlords"
};
for (String sample : samples) {
assertEquals(sample, fromHex(toHex(sample)));
}
}

Note: the stripping of leading 00 in fromHex is only necessary because of the "%040x" padding in your toHex method.
If you don't mind replacing that with a simple %x,
then you could drop this line in fromHex:

    hex = hex.replaceAll("^(00)+", "");

Convert a string representation of a hex dump to a byte array using Java?

Update (2021) - Java 17 now includes java.util.HexFormat (only took 25 years):

HexFormat.of().parseHex(s)


For older versions of Java:

Here's a solution that I think is better than any posted so far:

/* s must be an even-length string. */
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}

Reasons why it is an improvement:

  • Safe with leading zeros (unlike BigInteger) and with negative byte values (unlike Byte.parseByte)

  • Doesn't convert the String into a char[], or create StringBuilder and String objects for every single byte.

  • No library dependencies that may not be available

Feel free to add argument checking via assert or exceptions if the argument is not known to be safe.

Convert hex string to int

It's simply too big for an int (which is 4 bytes and signed).

Use

Long.parseLong("AA0F245C", 16);

HEX to String and back - value not matching

I rewrote the code to convert the String to Hexadecimal as

  public String convertStringToHex(String str){

char[] chars = str.toCharArray();

StringBuffer hex = new StringBuffer();
for(int i = 0; i < chars.length; i++){
hex.append(String.format("%02x", ((int)chars[i])));
}

return hex.toString();

}

and now it works correctly. The problem seems to that earlier it was ignoring the leading zero in the hexadecimal.

Convert hex string to binary string

You need to tell Java that the int is in hex, like this:

String hexToBinary(String hex) {
int i = Integer.parseInt(hex, 16);
String bin = Integer.toBinaryString(i);
return bin;
}

How to convert hex string to octal string in java

As per the discussion in the comments, you want to convert each byte individually and convert them to octal:

String s     = "My string to convert";
byte[] bytes = s.getBytes("UTF-8");

for (byte b : bytes) {
String octalValue = Integer.toString(b, 8);

// Do whatever
}

Hex string to binary in Java

I recommend you use a fairly simple method BigInteger::toString(int radix) which returns the String representation in the given radix. Use 2 for the binary representation.

// 100100011010001010110011110001001101010111100110111101111
new BigInteger("0123456789ABCDEF", 16).toString(2);

Note the String must be blank characters free and using this way you have to process them from the array each one-by-one.



Related Topics



Leave a reply



Submit