0
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class BinaryGUI extends BinaryGUIDemo { public static void main(String[] args) { new BinaryGUI(); } private JFrame frame; private JButton button; private JTextField text; private JTextField text2; private String decimalnumber = null; public BinaryGUI() { frame = new JFrame("Conversion!"); JLabel label = new JLabel("Binary:"); frame.setLayout(new FlowLayout()); frame.add(label); text = new JTextField(15); frame.add(text); JLabel label2 = new JLabel("Decimal:"); frame.add(label2); text2 = new JTextField(15); frame.add(text2); button = new JButton("Convert"); button.addActionListener(new ButtonListener()); frame.add(button); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(250, 300); frame.setLocationRelativeTo(null); frame.setVisible(true); } public void Convert() { String binary = text.getText(); int i; for (i = 0; i < binary.length(); i++) { char select = binary.charAt(binary.length() - i - 1); char number = (char) (select * Math.pow(2, i)); decimalnumber += number; } } public class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { Convert(); if (button.getText().equals("Convert")) { text2.setText(decimalnumber); text.getText(); text2.getText(); } else { } } } } 

I can run the program but for some reason it shows/ puts random characters instead of the initial value of the binary and I cant seem to run the program decimal back to binary. how can I improve this codes. To make it clear it does not convert binary to decimal, and how will I convert it back decimal to binary and if there is some codes that would help me, would be very appreciated.

3
  • 2
    You're operating on 'ascii' values, not on digits. You'd need char number = (char) ((select-'0') * Math.pow(2, i)); but there are better ways of doing this. Java method names begin lower case, btw, as do variable names: technojeeves.com/index.php/aliasjava1/… Commented Apr 24, 2022 at 13:40
  • You're working with char. Multiplication does not do what you think it does with char because the characters 1 and 0 do not correspond to the int values 1 and 0. As for converting decimal to binary, you haven't even implemented it, and there are plently of explanations online on how to do so, which you can easily find. Commented Apr 24, 2022 at 13:40
  • 0 converted to a char is not the value "0", it has the value "48". Have a look at the ASCII table Commented Apr 24, 2022 at 13:52

2 Answers 2

1

You are almost there, your problem is from the conversion from char to int. Characters in Java are stored in UTF-16 unicode. This means that under the hood the bit pattern does not match the value on screen. https://naveenr.net/unicode-character-set-and-utf-8-utf-16-utf-32-encoding/

So when you read in a char 1 this gets converted to number 49, 0 gets converted to 48

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class BinaryGUI { public static void main(String[] args) { new BinaryGUI(); } private JFrame frame; private JButton button; private JTextField text; private JTextField text2; private int decimalnumber = 0; public BinaryGUI() { frame = new JFrame("Conversion!"); JLabel label = new JLabel("Binary:"); frame.setLayout(new FlowLayout()); frame.add(label); text = new JTextField(15); frame.add(text); JLabel label2 = new JLabel("Decimal:"); frame.add(label2); text2 = new JTextField(15); frame.add(text2); button = new JButton("Convert"); button.addActionListener(new ButtonListener()); frame.add(button); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(250, 300); frame.setLocationRelativeTo(null); frame.setVisible(true); } public void Convert() { String binary = text.getText(); decimalnumber = 0; int i; for (i = 0; i < binary.length(); i++) { char select = binary.charAt(binary.length() - i - 1); System.out.println((int) select); // outputs the char as an int int number = (int) ((select - 48) * Math.pow(2, i)); decimalnumber += number; } } public class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { Convert(); text2.setText("" + decimalnumber); text.getText(); text2.getText(); } } } 

Note I made decimalnumber an int, then used string concatination to print it out

Sign up to request clarification or add additional context in comments.

Comments

0

Convert Binary to Decimal:

int deicimal = 12; String binaryString = Integer.toBinaryString(deicimal); // it will be "1100" 

Convert Decimal to Binary :

String binaryString = "1100"; int decimal = Integer.parseInt(binaryString, 2); // it will be 12 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.