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 7793961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:39:09+00:00 2026-06-01T22:39:09+00:00

i wrote a program to convert a hexadecimal number to its equivalent binary form

  • 0

i wrote a program to convert a hexadecimal number to its equivalent binary form using the following code but i got wrong results. The code is as follows:

public String convert(String num){
    String res="";
    int []hex={0000,0001,0010,0011,0100,0101,0110,0111,1000,1001,1010,1011,1100,1101,1110,1111};
    int i;
    char ch;
    for(i=0;i<num.length();i++)
    {
        ch=num.charAt(i);
        if(ch>='a' && ch<='f'){
            res+=hex[ch-97+10]+"";
        }
        else if(ch>='A' && ch<='F'){
            res+=hex[ch-65+10]+"";
        }
        else if(ch>='0' && ch<='9'){
            int d=ch-48;
            res+=hex[d]+"";
        }
    }
    return res;
}

If i give the sample input as “12ae” then i get the corresponding output as “1810101110”.
It happens only(goes wrong) when there is a number in the input field and works fine for all characters only. But when i change the array named hex to that of a String type it gives me the exact answer.

Is it because the compiler treats the numbers in the integer array as some form of octal numbers or is it because of some other reason?

  • 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-01T22:39:10+00:00Added an answer on June 1, 2026 at 10:39 pm

    You’re on the right track with the octal numbers. The following declaration:

    int []hex={0000,0001,0010,0011,0100,0101,0110,0111,1000,...};
    

    declares an array of integers that in words, would be

    zero
    one
    eight
    nine
    sixty-four
    sixty-five
    seventy-two
    seventy-three
    one thousand
    

    and so on. This is clearly not what you want. If you declare hex as String[] hex, and use "0000","0001",..., then you will get the intended answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote program to convert decimal to binary for practice purposes but i get
I wrote a program using AutoIT to fetch information from a number of websites
I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to
I want to convert function object to function. I wrote this code, but it
I'm trying to convert an old Delphi program I wrote into Java to compile
I wrote a program to click on an application automatically at scheduled time using
I wrote a program in C++/OpenGL (using Dev-C++ compiler) for my calculus 2 class.
I wrote a program in python (using standard python libraries) long ago. Now I
I am supposed to write program to convert infix notation to postfix notation using
I wrote a simple program, here's what it looks like, some details hidden: using

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.