Using some various Stackoverflow sources I’ve implemented a fairly simple Base64 to Hex conversion using JAVA. Due to a problem however I tested my results by attempting to convert my hex code back to text to confirm that it was correct and found that the character at index 11 (the left quote mark) is being lost in translation somehow.
Why does the hexToASCII convert everything except the left quote mark?
public static void main(String[] args){
System.out.println("Input string:");
String myString = "AAAAAQEAFxUX1iaTIz8=";
System.out.println(myString + "\n");
//toascii
String ascii = base64UrlDecode(myString);
System.out.println("Base64 to Ascii:\n" + ascii);
//tohex
String hex = toHex(ascii);
System.out.println("Ascii to Hex:\n" + hex);
String back2Ascii = hexToASCII(hex);
System.out.println("Hex to Ascii:\n" + back2Ascii + "\n");
}
public static String hexToASCII(String hex){
if(hex.length()%2 != 0){
System.err.println("requires EVEN number of chars");
return null;
}
StringBuilder sb = new StringBuilder();
//Convert Hex 0232343536AB into two characters stream.
for( int i=0; i < hex.length()-1; i+=2 ){
/*
* Grab the hex in pairs
*/
String output = hex.substring(i, (i + 2));
/*
* Convert Hex to Decimal
*/
int decimal = Integer.parseInt(output, 16);
sb.append((char)decimal);
}
return sb.toString();
}
public static String toHex(String arg) {
return String.format("%028x", new BigInteger(arg.getBytes(Charset.defaultCharset())));
}
public static String base64UrlDecode(String input) {
Base64 decoder = new Base64();
byte[] decodedBytes = decoder.decode(input);
return new String(decodedBytes);
}
Returns:

It doesn’t loose it. It doesn’t understand in your default charset. Use
arg.getBytes()instead, without specifying the charset.Also change
hexToAscIImethod: