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

  • Home
  • SEARCH
  • 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 8557139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:30:29+00:00 2026-06-11T15:30:29+00:00

This a code for converting hex to string but it works fine until size

  • 0

This a code for converting hex to string but it works fine until size of the string doesn’t exceeds 62 characters?

public static String hexToString(String hex)
        {       
           StringBuilder output = new StringBuilder();
           for (int i = 0; i < hex.length(); i+=2)
           {
            String str = hex.substring(i, i+2);
            output.append((char)Integer.parseInt(str, 16));
           }
           return(output.toString());
        }

java.lang.StringIndexOutOfBoundsException: String index out of range:
62
at java.lang.String.substring(Unknown Source)
at HEX.hexToString(HEX.java:36)
at HEX.main(HEX.java:56)

  • 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-11T15:30:30+00:00Added an answer on June 11, 2026 at 3:30 pm

    You will face this problem only when you have odd number of characters in your string. Fix your function as follows:

    public static String hexToString(String hex)
        {       
           StringBuilder output = new StringBuilder();
           String str = "";
           for (int i = 0; i < hex.length(); i+=2)
           {
    
            if(i+2 < hex.length()){
                str = hex.substring(i, i+2);
            }else{
                str = hex.substring(i, i+1);
            }
            output.append((char)Integer.parseInt(str, 16));
           }
           return(output.toString());
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this code for converting binary to text . public static void main(String
I need help converting this code from C# to Java. public static ulong GetChecksum(byte[]
This code : http://jsfiddle.net/bYtTG/1/ Works as I want in FF, chrome and opera, but
All my code works fine until i did some changes that i cant track
This HMACSHA1 code below works for converting Password and Message to AFF791FA574D564C83F6456CC198CBD316949DC9 as evidence
I have this code for converting a byte[] to float[] . public float[] ConvertByteToFloat(byte[]
i have this htaccess code for converting dynamic url to static url RewriteRule ^dogs/([^/.]+)$
Could you please help me converting this c++ code into python: I am trying
How do I prevent Cyclone from converting the code? I mean, I want this
I am having an issue converting type. I was trying code like this (minimal,

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.