Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Converting decimal to binary, octal & hex

normajeannormajean Posts: 39Member
I need to write a program that will convert decimals between 1 & 256 to binary, oct & hex numbers...I have no clue where to begin. I don't know the math I need to make these conversions. I understand the base 2, 8 and 16...but I don't know how to write the code to make these conversions. Any help would be great. This a homework assignment that is already a week past due, but I just can't find what I need to know...and the formulas were not given to us. The point of the assignment is to teach us a looping structure...I understand the looping...but can't complete the assigment without the math for the conversions. Help!

Here is my code as I have it now...
[code]
import javax.swing.*;

public class Equivalant {

public static void main(String[] args) {
//create text area to display table
JTextArea outputArea=new JTextArea(17,35);
//attach to scroller
JScrollPane scroller=new JScrollPane(outputArea);

String output;
int counter=1;
int temp;

//create table headers
output="Decimal Binary Octal Hexadecimal
";





while (counter <= 256)
//here is where I need my math





outputArea.setText(output);
JOptionPane.showMessageDialog(null, scroller, "Equivalent",
JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}//ends main

}//ends equivalent
[/code]

Thanks!

Comments

  • Alex_KAlex_K Posts: 1Member
    Just go ahead and use the functions from java.lang.Integer

    [code]
    import java.lang.Integer // doesn't need to be imported implicitly

    : import javax.swing.*;
    :
    : public class Equivalant {
    :
    : public static void main(String[] args) {
    : //create text area to display table
    : JTextArea outputArea=new JTextArea(17,35);
    : //attach to scroller
    : JScrollPane scroller=new JScrollPane(outputArea);
    :
    : String output;
    : int counter=1;
    : int temp;
    :
    : //create table headers
    : output="Decimal Binary Octal Hexadecimal
    ";
    :
    : while (counter <= 256)
    : //here is where I need my math
    output +=
    counter + ' ' +
    Integer.toBinaryString(counter) + ' ' +
    Integer.toOctalString(counter)) + ' ' +
    Integer.toHexString(counter)) + '
    ';
    // et voila
    :
    : outputArea.setText(output);
    : JOptionPane.showMessageDialog(null, scroller, "Equivalent",
    : JOptionPane.INFORMATION_MESSAGE);
    :
    : System.exit(0);
    :
    : }//ends main
    :
    : }//ends equivalent
    : [/code]

    If your interested in the actual conversion (not a big deal) just mail.

    Best regards


  • normajeannormajean Posts: 39Member
    Thanks, Alex. I was able to get it working, thanks to your help. I am not sure if my instructor wants to see the actual math, or if this will be acceptable. The program is running, and I think that was the point...not the math. If he tells me otherwise, I may be back. :)

    Thanks, again.
Sign In or Register to comment.