2

Please help me on converting a hexstring to base64

here is the cede where I'm getting the exception

 String hexString = "bf940165bcc3bca12321a5cc4c753220129337b48ad129d880f718d147a2cd1bfa79de92239ef1bc06c2f05886b0cd5d";

private static String ConvertHexStringToBase64(String hexString) {
    System.out.println(hexString);
    if ((hexString.length()) % 2 > 0)
        throw new NumberFormatException("Input string was not in a correct format.");
     byte[] buffer = new byte[hexString.length() / 2];
        int i = 0;
        while (i < hexString.length())
        {
            buffer[i / 2] = Byte.parseByte(hexString.substring(i, 2));
            i += 2;
        }
        System.out.println("hexSring"+hexString+"afterconverttobase64"+Base64.encodeBase64String(buffer));
        return Base64.encodeBase64String(buffer);

}

I'm getting an exception here::bf940165bcc3bca12321a5cc4c753220129337b48ad129d880f718d147a2cd1bfa79de92239ef1bc06c2f05886b0cd5d Exception in thread "main" java.lang.NumberFormatException: For input string: "bf" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:449) at java.lang.Byte.parseByte(Byte.java:151) at java.lang.Byte.parseByte(Byte.java:108) at com.motorola.gst.DecryptTest3.ConvertHexStringToBase64(DecryptTest3.java:38) at com.motorola.gst.DecryptTest3.main(DecryptTest3.java:16)

1

2 Answers 2

4

First of all you have to specify the specify the radix(16 in your case) in the parseByte method to avoid the numberFormat exception :

 buffer[i / 2] = Byte.parseByte(hexString.substring(i, 2),16);

However your code seems broken, take a look at the corrected one :

     if ((hexString.length()) % 2 > 0)
          throw new NumberFormatException("Input string was not in a correct format.");
       int[] buffer = new int[hexString.length() / 2];
          int i = 2;
          while (i < hexString.length())
          {
              buffer[i / 2] = Integer.parseInt(hexString.substring(i, i + 2),16);
              i += 2;
          }

Your loop was wrong and you have to parse as Integer because you have some values inside your input string that overflow the byte capability ...

If you need byte you could cast the parsed int to byte in this way :

       byte[] buffer = new byte[hexString.length() / 2];
          int i = 2;
          while (i < hexString.length())
          {
              buffer[i / 2] = (byte)Integer.parseInt(hexString.substring(i, i + 2),16);
              i += 2;
          }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks aleroot!! that was very much helpful
@aleroot in your second example, shouldn't you be initializing i to 0 instead of 2?
-1

Found a similar solution, thought it might be good to share:

    public static string convertHexToBase64String(String hexString)
    {
        string base64 = "";

        //--Important: remove "0x" groups from hexidecimal string--
        hexString = hexString.Replace("0x", "");

        byte[] buffer = new byte[hexString.Length / 2];

        for (int i = 0; i < hexString.Length; i++)
        {
            try
            {
                buffer[i / 2] = Convert.ToByte(Convert.ToInt32(hexString.Substring(i, 2), 16));
            }
            catch (Exception ex) { }
            i += 1;
        }

        base64 = Convert.ToBase64String(buffer);

        return base64;
    }

Hope it helps someone else.

1 Comment

good point about removing 0x as some firewalls will reject base65 containing 0x

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.