Binary to Hexadecimal Conversion

Discussion in 'Java' started by lei, Dec 30, 2006.

  1. lei

    lei Guest

    Hello! I made this program of converting binary to hexadecimal, there
    are few errors which are new to me. Please check it out. Thanks!

    <code>

    import java.lang.*;
    import java.io.*;

    class BinaryDecoder{
    public static void main(String args[]) throws IOException{
    InputStreamReader stdin = new InputStreamReader(System.in);
    BufferedReader console = new BufferedReader(stdin);
    System.out.print("Enter a number in binary: ");
    String input = console.readLine();


    int decimal=0;
    for(int counter=input.length()-1; counter>=0; counter--){
    if(input.charAt(counter)=='1'){
    int exp=input.length()-1-counter;
    decimal+=Math.pow(2,exp);
    }
    }

    int hexadecimal=0;
    int powerOfTen=1;
    int number=decimal;
    int counter=0;
    int[] hex = new int[20];

    while(number>0){
    int remainder=number%16;
    hex[counter] = remainder;
    counter++;
    number/=16;
    }

    System.out.print("Hexadecimal: ");
    for(int count=hex.length; count>=0; count--){
    if(hex[count]==10)
    System.out.print("A");
    else if(hex[count]==11)
    System.out.print("B");
    else if(hex[count]==12)
    System.out.print("C");
    else if(hex[count]==13)
    System.out.print("D");
    else if(hex[count]==14)
    System.out.print("E");
    else if(hex[count]==15)
    System.out.print("F");
    else
    System.out.print(hex[count]);
    }
    }
    }

    </code>
    lei, Dec 30, 2006
    #1
    1. Advertising

  2. lei

    lei Guest

    you can also suggest for improvements..thanks!
    lei, Dec 30, 2006
    #2
    1. Advertising

  3. lei wrote:
    > Hello! I made this program of converting binary to hexadecimal, there
    > are few errors which are new to me. Please check it out. Thanks!
    >
    > <code>
    >
    > import java.lang.*;
    > import java.io.*;
    >
    > class BinaryDecoder{
    > public static void main(String args[]) throws IOException{
    > InputStreamReader stdin = new InputStreamReader(System.in);
    > BufferedReader console = new BufferedReader(stdin);
    > System.out.print("Enter a number in binary: ");
    > String input = console.readLine();
    >
    >
    > int decimal=0;
    > for(int counter=input.length()-1; counter>=0; counter--){
    > if(input.charAt(counter)=='1'){
    > int exp=input.length()-1-counter;
    > decimal+=Math.pow(2,exp);
    > }
    > }
    >
    > int hexadecimal=0;
    > int powerOfTen=1;
    > int number=decimal;
    > int counter=0;
    > int[] hex = new int[20];
    >
    > while(number>0){
    > int remainder=number%16;
    > hex[counter] = remainder;
    > counter++;
    > number/=16;
    > }
    >
    > System.out.print("Hexadecimal: ");
    > for(int count=hex.length; count>=0; count--){
    > if(hex[count]==10)
    > System.out.print("A");
    > else if(hex[count]==11)
    > System.out.print("B");
    > else if(hex[count]==12)
    > System.out.print("C");
    > else if(hex[count]==13)
    > System.out.print("D");
    > else if(hex[count]==14)
    > System.out.print("E");
    > else if(hex[count]==15)
    > System.out.print("F");
    > else
    > System.out.print(hex[count]);
    > }
    > }
    > }
    >
    > </code>
    >


    How about you tell us the errors you have found so far instead of
    relying on us to scan your code and/or compile it to generate the errors?
    Brandon McCombs, Dec 30, 2006
    #3
  4. lei wrote:
    > I made this program of converting binary to hexadecimal,
    > there are few errors which are new to me.


    > for(int count=hex.length; count>=0; count--){
    > if(hex[count]==10)


    You are addressing the array 'hex' beyond the end.
    Thomas Schodt, Dec 30, 2006
    #4
  5. lei

    lei Guest

    Thomas Schodt wrote:
    > lei wrote:
    > > I made this program of converting binary to hexadecimal,
    > > there are few errors which are new to me.

    >
    > > for(int count=hex.length; count>=0; count--){
    > > if(hex[count]==10)

    >
    > You are addressing the array 'hex' beyond the end.


    please explain further and suggest a solution(if it's okay), thank you
    so much!
    lei, Dec 30, 2006
    #5
  6. lei

    lei Guest

    It's working now. Another problem:

    Enter a number in binary: 10000011
    Hexadecimal: 00000000000000000083

    How can I remove the zeros?
    lei, Dec 30, 2006
    #6
  7. lei

    Daniel Dyer Guest

    On Sat, 30 Dec 2006 09:31:44 -0000, lei <> wrote:

    > you can also suggest for improvements..thanks!


    It could be so much simpler:

    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html#parseInt(java.lang.String,
    int)
    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html#toHexString(int)

    Everything after reading the input could be replaced with this one line:

    Integer.toHexString(Integer.parseInt(input, 2));

    Dan.

    --
    Daniel Dyer
    https://watchmaker.dev.java.net - Evolutionary Algorithm Framework for Java
    Daniel Dyer, Dec 30, 2006
    #7
  8. lei

    lei Guest

    Daniel Dyer wrote:
    > On Sat, 30 Dec 2006 09:31:44 -0000, lei <> wrote:
    >
    > > you can also suggest for improvements..thanks!

    >
    > It could be so much simpler:
    >
    > http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html#parseInt(java.lang.String,
    > int)
    > http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html#toHexString(int)
    >
    > Everything after reading the input could be replaced with this one line:
    >
    > Integer.toHexString(Integer.parseInt(input, 2));
    >
    > Dan.
    >
    > --
    > Daniel Dyer
    > https://watchmaker.dev.java.net - Evolutionary Algorithm Framework for Java


    I can't use it. The idea is to create your own toHexString.
    lei, Dec 30, 2006
    #8
  9. lei

    Daniel Dyer Guest

    On Sat, 30 Dec 2006 14:13:50 -0000, lei <> wrote:

    >
    > Daniel Dyer wrote:
    >> On Sat, 30 Dec 2006 09:31:44 -0000, lei <> wrote:
    >>
    >> > you can also suggest for improvements..thanks!

    >>
    >> It could be so much simpler:
    >>
    >> http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html#parseInt(java.lang.String,
    >> int)
    >> http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html#toHexString(int)
    >>
    >> Everything after reading the input could be replaced with this one line:
    >>
    >> Integer.toHexString(Integer.parseInt(input, 2));
    >>
    >> Dan.

    >
    > I can't use it. The idea is to create your own toHexString.


    OK, that wasn't immediately clear.

    Dan.

    --
    Daniel Dyer
    https://watchmaker.dev.java.net - Evolutionary Algorithm Framework for Java
    Daniel Dyer, Dec 30, 2006
    #9
  10. lei

    Lew Guest

    lei wrote:
    > It's working now. Another problem:
    >
    > Enter a number in binary: 10000011
    > Hexadecimal: 00000000000000000083
    >
    > How can I remove the zeros?


    A very good question!

    Take yourself off the keyboard for a few minutes, maybe actually use a
    pen[cil] and paper to work through the inherent logic of the challenge.

    Think along the lines of, "Hmm, what conditions pertain when a am preparing to
    output a '0' that would require me to display it? What conditions would allow
    me to suppress it?" (Hint: One of the conditions is that the numeral to
    display at the moment is '0'.)

    Then detect those conditions in your code, and either emit or decline to emit
    the '0' as appropriate.

    - Lew
    Lew, Dec 30, 2006
    #10
  11. lei

    Don Roby Guest

    lei wrote:
    > you can also suggest for improvements..thanks!
    >


    The biggest improvement would be to introduce some functions instead of
    just having one long main method. Introducing objects might be nice as
    well, but I suspect this is an exercise pretty early in your class.

    The loop converting the binary string to an int should be an int-valued
    function with a string argument. The computations in this loop could
    also be done without calls to Math.pow. (Hint: You only need
    multiplications by 2 and additions.)

    It might also be nice to detect overflow here and give an error if the
    user enters something too large for an int. As it stands, the code just
    gives the result for the maximum int value if something larger is
    entered. This could be out of scope for an introductory level
    assignment though.

    A 20 digit hex string is far larger than the maximum int value, so
    you'll always have leading zeros with this process unless you detect
    them and don't print them.

    But the int[20] array really shouldn't be needed at all. Think about
    how to build a string from your remainders as you go instead of saving
    them in an array.

    And again, this really should be a function rather than a loop inside main.

    Finally, if you really want to impress your teacher, think about going
    straight from binary string to hex string without ever producing an int,
    thereby not needing to worry about those integer overflows. This is
    actually not hard for going from binary to hex or octal. Other bases
    would be much messier.
    Don Roby, Dec 30, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Hannak
    Replies:
    0
    Views:
    7,443
    Victor Hannak
    Feb 20, 2004
  2. Bengt Richter

    Hexadecimal Conversion in Python

    Bengt Richter, Nov 2, 2005, in forum: Python
    Replies:
    10
    Views:
    51,202
    Peter Hansen
    Nov 2, 2005
  3. hexadecimal to float conversion

    , Aug 19, 2005, in forum: C Programming
    Replies:
    4
    Views:
    1,719
    Keith Thompson
    Aug 20, 2005
  4. SM Ryan

    Re: hexadecimal to float conversion

    SM Ryan, Aug 20, 2005, in forum: C Programming
    Replies:
    3
    Views:
    571
    Keith Thompson
    Aug 25, 2005
  5. Deepu

    Binary to Hexadecimal conversion

    Deepu, Dec 15, 2007, in forum: Perl Misc
    Replies:
    5
    Views:
    180
Loading...

Share This Page