如何将十六进制转换为十进制和从十进制转换为二进制

How to convert from Hexadecimal to Decimal and from Decimal to Binary

我想知道如何将舞蹈法的小数结果包含到光法中。例如,在这个程序中,如果我输入 5F,十进制结果将是 95。那么,我希望这个 95 在 light 方法中显示为一个 static int 变量,以便转换为二进制数。如果您能告诉我如何将十六进制数限制为仅 2 位数字,那也会非常有帮助。谢谢阅读!

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));
      }
    }

欢迎来到 Whosebug,

如果您想将十六进制值转换为普通整数,您可以使用:

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

如果您希望将纯整数转换为二进制字符串,您可以使用:

String j = Integer.toBinaryString(i); // from the above variable j which contains 95
System.out.println(j); // will print 1011111