1

I have a Base64 String, which is 'AAAC', which in binary is equivalent to 3 bytes (00000000 00000000 00000010).

I would like to convert 'AAAC' to a ASCII Hex Byte String to output something like '000002'.

I have tried below.

byte[] test = Base64.decode(data.getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : test) {
    sb.append(String.format("%02X ", b));
}
System.out.println(sb.toString()); 
//Output: 00 00 02 

This works, but is there a more effient way to do this?

Any help would be appreciated. Thanks in advance.

0

2 Answers 2

2

You could use the DatatypeConverter class for this:

String result =
    DatatypeConverter.printHexBinary(DatatypeConverter
        .parseBase64Binary("AAAC"));

System.out.println(result);

Prints: 000002

Sign up to request clarification or add additional context in comments.

Comments

1

With the Guava library, this might look something like

BaseEncoding.base16().toUpperCase().withSeparator(" ", 2)
  .encode(BaseEncoding.base64().decode(data));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.