user3019802
user3019802

Reputation: 75

How do I translate this snippet of code into something a GUI could read

I'm trying to create a equation that will translate a hexadecimal to a binary number. I know what the code is in the standard editor, I just don't know how to translate it so for a GUI

This is my attempt to create it in a GUI environment.

    // -------------------------------------------------------------
// Equation  for Binary Conversion
//--------------------------------------------------------------

public void binaryConversion (double binary){
    String binary = Integer.toBinaryString(decimal);
    totalLabel.setText(decimal.format(binary));

This is the code that translates a hexadecimal to a binary number

String binary = Integer.toBinaryString(decimal);

If I didn't have to make a GUI, I would simply System.out.print these methods. I'm confused. So far I created a GUI, it's just non functioning. I need to make it function. I have a fairly good idea on how to implement actionlisteners, and buttons listeners. So I think I'll be okay with that. It's just translating these equations for a GUI that's confusing me. Any help would be appreciated.

I also need to convert a hexadecimal to a decimal.

EDIT

My attempt to use NumberFormatException. Still getting errors though.

(this is probably completely off.

public void binaryConversion (double binary){
        NumberFormat b = NumberFormat.getInstance(b, 16);
        Integer.toBinaryString();

Upvotes: 0

Views: 93

Answers (1)

Scary Wombat
Scary Wombat

Reputation: 44834

For a start, does this code compile?

public void binaryConversion (double binary){
String binary = Integer.toBinaryString(decimal);

It is having both a parameter and a local variable called binary.

Update

You can use

   try {
     Integer b = Integer.valueOf(hexString,16);
     Integer.toBinaryString(b);
   } catch (NumberFormatException ee) {
     ee.printStackTrace();
   }

Upvotes: 1

Related Questions