Java十进制转成二进制 八进制 十六进制

时间:2023-03-09 18:49:38
Java十进制转成二进制 八进制 十六进制
int a = 357;
//十进制转成二进制
System.out.println(Integer.toBinaryString(a));
package com.swift;

import java.util.Scanner;

public class Decimal2Binary {

    public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("please enter a Integer:");
int integer=scan.nextInt();
String octal=Integer.toBinaryString(integer);
System.out.println(octal);
}
}
//十进制转成八进制
System.out.println(Integer.toOctalString(a));
package com.swift;

import java.util.Scanner;

public class Decimal2Octal {

    public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("please enter a Integer:");
int integer=scan.nextInt();
String octal=Integer.toOctalString(integer);
System.out.println(octal);
}
}
//十进制转成十六进制
System.out.println(Integer.toHexString(a));
import java.util.Scanner;

public class Decimal2Hex {

    public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("please enter a Integer:");
int integer=scan.nextInt();
String hex=Integer.toHexString(integer);
System.out.println(hex);
}
}