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.
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)));
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With