Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8679067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:52:43+00:00 2026-06-12T20:52:43+00:00

Please help me on converting a hexstring to base64 here is the cede where

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:52:44+00:00Added an answer on June 12, 2026 at 8:52 pm

    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;
              }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please help me why i'm getting this error. I'm getting Json response converting into
Could you please help me converting this c++ code into python: I am trying
Please help me somebody to convert unicodestring into string This is how i am
Can someone please help with converting this code to plain JS: $(document).ready(function() { $(textarea).bind(keydown,
Could you please help me with such converting I have NSString date like @2/8/2012
Hi stackoverflow team i have a problem in converting base64 string to bitmap in
Could you please help me in converting date from the format 20120101 to DATE
I need help converting this into PHP: Public Function Encrypt(ByVal text As String) As
Can anyone help me with a way of converting String to double in vc++?
I have a problem on converting JSON string to object in Android. Here are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.