5

I have one Base64 String YxRfXk827kPgkmMUX15PNg== I want to convert it into 63145F5E4F36EE43E09263145F5E4F36

So I think scenario would be like this I have to first decode Base64 string and than convert it into Hex

My code is given below

import org.apache.commons.codec.binary.Base64;

String guid = "YxRfXk827kPgkmMUX15PNg==";
byte[] decoded = Base64.decodeBase64(guid);
try {
    System.out.println(new String(decoded, "UTF-8") + "\n");
} catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
}

Above code gives c_^O6?C??c_^O6

But I don't know How to convert this string into Hex string. So it gives the 63145F5E4F36EE43E09263145F5E4F36 output.

So please help me to fix this issue.

user3441151
  • 1,880
  • 6
  • 35
  • 79
  • You encoded the bytes in UTF-8. What I guess you want is a string-representation of the byte values in hex. – Fildor Jan 12 '17 at 10:57
  • 1
    you may want to look into the [DatatypeConverter](http://docs.oracle.com/javase/7/docs/api/javax/xml/bind/DatatypeConverter.html).parseBase64Binary() and printHexBinary() – Rhayene Jan 12 '17 at 10:57

2 Answers2

12

Since you are already using Apache Common Codec:

String guid = "YxRfXk827kPgkmMUX15PNg==";
byte[] decoded = Base64.decodeBase64(guid);
String hexString = Hex.encodeHexString(decoded);
System.out.println(hexString);

Using standard Java libraries:

String guid = "YxRfXk827kPgkmMUX15PNg==";
byte[] decoded = Base64.getDecoder().decode(guid);
System.out.println(String.format("%040x", new BigInteger(1, decoded)));
Shakti Dash
  • 131
  • 3
1

Hey try this code it gives the expected output

import java.util.Base64;

/**
*
* @author hemants
*/
public class NewClass5 {

    public static void main(String[] args) {
        String guid = "YxRfXk827kPgkmMUX15PNg==";
        byte[] decoded = Base64.getDecoder().decode(guid);
        System.out.println(toHex(decoded));
    }
    private static final char[] DIGITS
            = {'0', '1', '2', '3', '4', '5', '6', '7',
                '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    public static final String toHex(byte[] data) {
        final StringBuffer sb = new StringBuffer(data.length * 2);
        for (int i = 0; i < data.length; i++) {
            sb.append(DIGITS[(data[i] >>> 4) & 0x0F]);
            sb.append(DIGITS[data[i] & 0x0F]);
        }
        return sb.toString();
    }

}

Output

63145F5E4F36EE43E09263145F5E4F36
Hemant Sangle
  • 282
  • 3
  • 19
  • That's very very similar to the solution in the duplicate answer. – Fildor Jan 12 '17 at 11:04
  • what advantage do you see in your example - to using standard lib methods instead? – Rhayene Jan 12 '17 at 11:05
  • @Rhayene Well I have used this function for myself long time ago saw the question and on giving this input `String guid = "YxRfXk827kPgkmMUX15PNg==";` to the function gives the expected output so I answered the question – Hemant Sangle Jan 12 '17 at 11:10
  • I am just wondering, why everyone uses such complicated functions to create a hex-string from an byte[]. – Rhayene Jan 12 '17 at 11:14
  • @Rhayene In the comments to the dupe answer it is stated (I haven't checked myself) that using such a function is about 2x faster than DatatypeConverter. – Fildor Jan 12 '17 at 11:17
  • @Fildor thanks, that I was looking for - but I guess the difference will be in a range that you optimize this quite late - after eradicating the bigger performance issues. – Rhayene Jan 12 '17 at 11:22