1

I would like how can I include the decimal result from the dance method to the light one. For example, in this program, if I input 5F, the decimal result would be 95. Well, I want that 95 to appear as a static int variable in the light method in order to be converted into a binary number. It would be also very helpful if you could tell me how can I limit the hexadecimal number to only 2 figures. Thanks for reading!

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class test2 {
public static void main(String args[]) throws Exception{
        DANCE(args);
        LIGHTS(args);

}

      public static void DANCE(String[]args) throws Exception {
            BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter hex no:"+"");
            int no= Integer.parseInt(reader.readLine(), 16);
            System.out.println("Decimal no:"+no);

}

       public static void LIGHTS(String a[]){
                System.out.println("Binary representation: ");
                System.out.println(Integer.toBinaryString(no));
      }
    }
ankuranurag2
  • 2,300
  • 15
  • 30

1 Answers1

2

Welcome to Stackoverflow,

if you want to convert a hexadecimal value to a plain integer you can use:

int i = Integer.parseInt("5F", 16);
System.out.println(i); // will print 95

And if you want your plain integer to be converted into a binary String you could use:

String j = Integer.toBinaryString(i); // from the above variable j which contains 95
System.out.println(j); // will print 1011111
Reporter
  • 3,897
  • 5
  • 33
  • 47
smsnheck
  • 1,563
  • 3
  • 21
  • 33