Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Hex to Binary Converter Help

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Location
    Jersey City, New Jersey
    Posts
    10
    My Mood
    Mad
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Hex to Binary Converter Help

    This code is for a GUI Java program that is supposed to convert back and fourth between two numbering systems. For example, binary to decimal or decimal to binary. I have created methods for some of the conversions however, I could successfully develop a method to convert from hex to binary. If you plan to run it to see what happens to the current method that I made please know that in the GUI the north end from left to right is as follows. The textfield is for user entry of any type of data. The first combobox is for the user specifying to the program what type of data he or she entered. The second combobox is for the user to choose what he wants that data converted to. The button is to convert it. The south side has a textarea that gives out the results. Please note that only some of the conversions work so far. There are comments in the code to label which methods do what converisons. Please help me find a method that will convert from hex to binary or please tell me what is wrong with the current method. For more information, please just question me with a post. Thanks guys.

     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
    import javax.swing.text.Highlighter;
     
    public class converter extends JFrame implements ActionListener
    {	
    	private JPanel box;
    	private JPanel flow;
    	private JTextField input;
    	private JComboBox convsel;
    	private JComboBox convoutsel;
    	private JButton subconvert;
    	private JTextArea output;
    	private String[] conversions; 
     
    	private converter()
    	{
    		super("Number Systems Converter");
     
    		conversions = new String[]{"Binary" , "Decimal", "Hex"};
     
    		box = new JPanel();
    		box.setLayout(new BoxLayout(box, BoxLayout.PAGE_AXIS));
     
    		flow = new JPanel();
    		flow.setLayout(new FlowLayout());
     
    		input = new JTextField();
    		convsel = new JComboBox(conversions);
    		convoutsel = new JComboBox(conversions);
    		subconvert = new JButton("Convert");
    		output = new JTextArea();
     
    		output.setEditable(false);
    		output.setText("Output");
     
    		input.setPreferredSize(new Dimension(200, (int)convsel.getPreferredSize().getHeight()));
    		input.setEditable(true);
     
    		convoutsel.setPreferredSize(new Dimension(150, (int)convsel.getPreferredSize().getHeight()));
     
    		convsel.setPreferredSize(new Dimension(150, (int)convsel.getPreferredSize().getHeight()));
     
    		output.setPreferredSize(new Dimension((int)output.getPreferredSize().getWidth(), 410));
     
    		subconvert.addActionListener(this);
     
    		flow.add(input);
    		flow.add(convsel);
    		flow.add(convoutsel);
    		flow.add(subconvert);
     
    		JScrollPane scrolloutput = new JScrollPane(output);
    		scrolloutput.setPreferredSize(new Dimension((int)output.getPreferredSize().getWidth() -50, 410 -50));
     
    		box.add(flow, BorderLayout.NORTH);
    		box.add(scrolloutput, BorderLayout.SOUTH);
     
    		add(box, BorderLayout.NORTH);
    	}
     
    	private boolean binary()
    	{
    		if(convsel.getSelectedItem() != "Binary")
    			return false;
    		return true;
    	}
     
    	private boolean decimal()
    	{
    		if(convsel.getSelectedItem() != "Decimal")
    			return false;
    		return true;
    	}
     
    	private boolean hex()
    	{
    		if(convsel.getSelectedItem() != "Hex")
    			return false;
    		return true;
    	}
     
    	private void convert()
    	{
    		if(convsel.getSelectedItem() == convoutsel.getSelectedItem())
    		{
    			SwingUtilities.invokeLater(
    					new Runnable()
    					{
    						public void run()
    						{
    							output.append("\nNumbers of the same type do not result in a conversion.");
    						}
    					}
    					);
    		}
     
    		//converts from binary to decimal
     
    		if(binary() == true && convoutsel.getSelectedItem() == "Decimal")
    		{
    			final int decimal = Integer.parseInt(input.getText(), 2);
     
    			SwingUtilities.invokeLater(
    					new Runnable()
    					{
    						public void run()
    						{
    							output.append("\nThe conversion from " + convsel.getSelectedItem() + " to " + convoutsel.getSelectedItem() + " for the input of " + input.getText().toString() + " resulted in the output of " + Integer.toString(decimal) + ".");
    						}
    					}
    					);
    		}
     
    		//converts from decimal to binary
     
    		if(decimal() == true && convoutsel.getSelectedItem() == "Binary")
    		{
    			final int bin = Integer.parseInt(input.getText());
    			final String binstring = Integer.toBinaryString(bin);
     
    			SwingUtilities.invokeLater(
    					new Runnable()
    					{
    						public void run()
    						{
    							output.append("\nThe conversion from " + convsel.getSelectedItem() + " to " + convoutsel.getSelectedItem() + " for the input of " + input.getText().toString() + " resulted in the output of " + binstring + ".");
    						}
    					}
    					);
    		}
     
    		//converts from binary to hex
     
    		if(binary() == true && convoutsel.getSelectedItem() == "Hex")
    		{
    			final int binary = Integer.parseInt(input.getText(), 2);
    			final String hexstring = Integer.toHexString(binary);
     
    			SwingUtilities.invokeLater(
    					new Runnable()
    					{
    						public void run()
    						{
    							output.append("\nThe conversion from " + convsel.getSelectedItem() + " to " + convoutsel.getSelectedItem() + " for the input of " + input.getText().toString() + " resulted in the output of " + hexstring + ".");
    						}
    					}
    					);
    		}
     
    		//converts from hex to binary
     
    		if(hex() == true && convoutsel.getSelectedItem() == "Binary")
    		{
    			final int hex = Integer.parseInt(input.getText(), 16);
    			final String binary = Integer.toBinaryString(hex);
     
    			SwingUtilities.invokeLater(
    					new Runnable()
    					{
    						public void run()
    						{
    							output.append("\nThe conversion from " + convsel.getSelectedItem() + " to " + convoutsel.getSelectedItem() + " for the input of " + input.getText().toString() + " resulted in the output of " + binary + ".");
    						}
    					}
    					);
    		}
    	}
     
    	public void actionPerformed(ActionEvent event)
    	{
    		if(event.getSource() == subconvert)
    		{
    			convert();
    		}
    	}
     
    	public static void main(String[] args)
    	{
    		converter object = new converter();
    		object.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		object.setVisible(true);
    		object.setSize(625,450);
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Hex to Binary Converter Help

    Can you show what problem you are having?
    Post the input to the program and the output, explain what is wrong and show what you want the output to be.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Location
    Jersey City, New Jersey
    Posts
    10
    My Mood
    Mad
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hex to Binary Converter Help

    This is the current output of the code:

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "0xC"
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at converter.convert(converter.java:162)
    at converter.actionPerformed(converter.java:181)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    I got this by doing the following:

    0xC is what went inside the input textfield.
    Select hex for the first dropdown box.
    Select binary for the second dropdown box.
    And Press the Convert Button.

    Which gives me the error up top, however, 0xC is a hex number so it is supposed use the method to convert that into a binary, in this case since thats what was in the second dropdown box. The full output if it did not give me the error would be:

    "The conversion from Hex to Binary for the input of 0xC resulted in the output of 1100."

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Hex to Binary Converter Help

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "0xC"
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at converter.convert(converter.java:162)
    0xC is Not a valid hex number. The hex digits are 0-9 and a-f

    If you want to allow 0x, the code needs to explicitly test for the leading 0x and strip it off before trying to convert it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. hex to binary - how do i flush off the leading 0
    By kennylty in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 5th, 2014, 07:32 PM
  2. Replies: 1
    Last Post: May 13th, 2013, 05:09 PM
  3. Replies: 7
    Last Post: January 23rd, 2013, 09:04 PM
  4. [SOLVED] Making Binary Converter script from scratch, running into math issue.
    By mwebb in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 8th, 2011, 07:47 PM
  5. Binary datafile and object creation according to binary tag ?
    By loicus in forum Object Oriented Programming
    Replies: 4
    Last Post: October 14th, 2011, 01:00 PM