Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 13th, 2011, 3:37 AM   #1
kgqmbqrq
Banned
 
Join Date: May 2011
Posts: 39
Rep Power: 0 kgqmbqrq is on a distinguished road
Convert Decimal to Binary, Octal and Hexadecimal

Hello guys, can someone help me.. our task is to input a Decimal Number and the output will be Binary,Octal and Hexadecimal..we can use Scanner, BufferedReader of JOptionPane but i prefer JOptionPane to use.. i would appreciate it very much!.tnx a lot.
kgqmbqrq is offline   Reply With Quote
Old Jul 13th, 2011, 4:39 AM   #2
macteki
Professional Programmer
 
Join Date: Jul 2011
Posts: 437
Rep Power: 4 macteki will become famous soon enough
Re: Convert Decimal to Binary, Octal and Hexadecimal

Compile and run the following program.
Enter any number in the console.

// file : Converter.java
class Converter
{
  public static void main(String[] args) throws Exception
  {
    java.io.BufferedReader br = new java.io.BufferedReader(
      new java.io.InputStreamReader(System.in)
    );

    String line = br.readLine();
    int decimal = 0;
    try { 
      decimal = Integer.parseInt(line);
    } catch (Exception e) 
    {
      System.out.println("Invalid number !");
      System.exit(1);
    }
    

    System.out.println("dec="+decimal);
    System.out.println("bin="+Integer.toString(decimal,2));
    System.out.println("oct="+Integer.toString(decimal,8));
    System.out.println("hex="+Integer.toString(decimal,16));

  }
}
__________________
API belongs to public. Knowledge belongs to yourself

http://java.macteki.com
macteki is offline   Reply With Quote
Old Jul 13th, 2011, 4:59 AM   #3
macteki
Professional Programmer
 
Join Date: Jul 2011
Posts: 437
Rep Power: 4 macteki will become famous soon enough
Re: Convert Decimal to Binary, Octal and Hexadecimal

Or, you may write your own converter for maximum flexibility. (and for educational purpose)

// file OwnConverter.java
class OwnConverter
{

  public static String myBinaryConverter(int decimal)
  {
    String s="";

    int bitmask = 0x80000000;
    while (bitmask!=0)
    {
      if ((decimal & bitmask) == 0) s+="0";  else s+="1";
      bitmask >>>= 1;
    }
    return s;
  }

  public static String myHexConverter(int decimal)
  {
    String[] digits = new String[]
      {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};

    String sign="";
    if (decimal<0) 
    {
      sign="-";
      decimal = -decimal;
    }

    StringBuffer s=new StringBuffer();

    while (decimal!=0)
    {
      s.append(digits[ decimal % 16 ]);
      decimal /= 16;
    }

    return sign+s.reverse();
  }


  public static String myOctConverter(int decimal)
  {
    String[] digits = new String[]
      {"0","1","2","3","4","5","6","7","8"};

    String sign="";
    if (decimal<0) 
    {
      sign="-";
      decimal = -decimal;
    }

    StringBuffer s=new StringBuffer();

    while (decimal!=0)
    {
      s.append(digits[ decimal % 8 ]);
      decimal /= 8;
    }

    return sign+s.reverse();
  }


  public static void main(String[] args) throws Exception
  {
    java.io.BufferedReader br = new java.io.BufferedReader(
      new java.io.InputStreamReader(System.in)
    );

    String line = br.readLine();
    int decimal = 0;
    try { 
      decimal = Integer.parseInt(line);
    } catch (Exception e) 
    {
      System.out.println("Invalid number !");
      System.exit(1);
    }
    

    System.out.println("dec="+decimal);
    System.out.println("bin="+myBinaryConverter(decimal));
    System.out.println("oct="+myOctConverter(decimal));
    System.out.println("hex="+myHexConverter(decimal));

  }
}
__________________
API belongs to public. Knowledge belongs to yourself

http://java.macteki.com
macteki is offline   Reply With Quote
Old Jul 13th, 2011, 6:19 AM   #4
macteki
Professional Programmer
 
Join Date: Jul 2011
Posts: 437
Rep Power: 4 macteki will become famous soon enough
Re: Convert Decimal to Binary, Octal and Hexadecimal

If you want GUI, this 110 lines program is a starting point for you. It shows how to use typical swing components, from which you may learn a lot if you are a beginner.
// SwingConverter.java
class SwingConverter implements java.awt.event.ActionListener
{

  private javax.swing.JPanel inputPanel = initInputPanel();
  private javax.swing.JTextField inputText;
  private javax.swing.JTextArea resultArea;
  private javax.swing.JButton enterButton;

  public javax.swing.JPanel initInputPanel()
  {
    javax.swing.JPanel panel=new javax.swing.JPanel();

    panel.setPreferredSize(new java.awt.Dimension(640,480));
    panel.setLayout(null);

    javax.swing.JLabel label = new javax.swing.JLabel("Enter a number : ");
    label.setBounds(50,50,100,30);
    panel.add(label);

    javax.swing.JTextField text;
    text = inputText = new javax.swing.JTextField();
    text.setBounds(180,50,100,30);
    panel.add(text);

    javax.swing.JButton button = new javax.swing.JButton("Enter");
    button.setActionCommand("Enter");
    button.setBounds(290,50,80,30);
    button.addActionListener(this);
    panel.add(button);
    enterButton = button;   // save a global copy

    button = new javax.swing.JButton("Reset");
    button.setActionCommand("Reset");
    button.setBounds(390,50,80,30);
    button.addActionListener(this);
    panel.add(button);

    resultArea = new javax.swing.JTextArea(); 
    resultArea.setBounds(50,180,500,200);
    resultArea.setEditable(false);
resultArea.setBackground(java.awt.Color.CYAN);
    panel.add(resultArea);

    return panel;
  }  

  public SwingConverter()
  {
    javax.swing.JFrame window = new javax.swing.JFrame("Base Converter");
    window.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    
    window.add(inputPanel);
    window.pack();
    window.setVisible(true);
  }

  private Integer getInputNumber()
  {
    try {
      Integer n = Integer.parseInt(inputText.getText());
      return n;
    }
    catch (Exception e)
    {
      return null;
    }
  }

  private void showResult()
  {
    inputText.setEnabled(false);
    enterButton.setEnabled(false);
    
    Integer n=getInputNumber();
    if (n==null)
    {
      resultArea.append("Invalid number !");     
      return;
    }

    int decimal = n.intValue();
    resultArea.append("dec = "+decimal+"\n");
    resultArea.append("bin = "+Integer.toString(decimal,2)+"\n");
    resultArea.append("oct = "+Integer.toString(decimal,8)+"\n");
    resultArea.append("hex = "+Integer.toString(decimal,16)+"\n");

  }

  public void actionPerformed(java.awt.event.ActionEvent ev)
  {
    String command=ev.getActionCommand();
    if (command.equals("Enter"))
    {
      showResult();    
    }
    else if (command.equals("Reset"))
    {
      inputText.setText("");
      resultArea.setText("");
      inputText.setEnabled(true);
      enterButton.setEnabled(true);
      inputText.requestFocus();
    }
  }

  public static void main(String[] args) throws Exception
  {
    new SwingConverter();
  }
}
__________________
API belongs to public. Knowledge belongs to yourself

http://java.macteki.com
macteki is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
understanding octal input by python compiler obir Python 1 Jul 13th, 2010 4:44 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:39 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC