Reputation: 39
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.
Upvotes: 0
Views: 102
Reputation: 89
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
Upvotes: 1
Reputation: 1696
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
Upvotes: 0