-2

I am getting a base64 string. How do I convert it to hex. I tried the followind but it isn't working

String guid = "YxRfXk827kPgkmMUX15PNg==";
byte[] decoded = Base64.decodeBase64(guid);
String hexString = Hex.encodeHexString(decoded);
System.out.println(hexString);
user2433953
  • 121
  • 1
  • 2
  • 7

1 Answers1

1
String guid = "YxRfXk827kPgkmMUX15PNg==";
byte[] decoded = Base64.decodeBase64(guid);
String result = HexUtil.toHex(decoded);

the hexUtil class :

public class HexUtil{ 
    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 StringBuilder sb = new StringBuilder();
        for (byte d : data) {
            sb.append(DIGITS[(d >>> 4) & 0x0F]);
            sb.append(DIGITS[d & 0x0F]);
        }
        return sb.toString();
    }
}
Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35