#### Howdy, Stranger!

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

# Converting decimal to binary, octal & hex

Member Posts: 39
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;

";

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!

• Member Posts: 1
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;
:
";
:
: 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

• Member Posts: 39
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.
• USAMember Posts: 4,624 ✭✭

__ \ http://forcoder.org \ free ebooks and video tutorials about < PL/SQL PHP Go JavaScript R C Scratch Java C++ Delphi C# Assembly Visual Basic Objective-C Visual Basic .NET Python Swift Ruby MATLAB Perl F# Alice Scheme Apex Bash Lua Clojure Fortran Julia Awk ABAP FoxPro Prolog ML Rust Scala Kotlin LabVIEW Logo D Lisp Ada Dart Erlang Transact-SQL Crystal SAS Hack VBScript COBOL /> __________