In Java, binary, octal, and hexadecimal are converted to each other in decimal format.
Last Update:2018-12-05
Source: Internet
Author: User
Convert decimal to hexadecimal:
Integer.toHexString(int i)
Decimal to octal
Integer.toOctalString(int i)
Convert decimal to binary
Integer.toBinaryString(int i)
Hexadecimal to decimal
Integer.valueOf("FFFF",16).toString()
Convert octal to decimal
Integer.valueOf("876",8).toString()
Two-to-decimal
Integer.valueOf("0101",2).toString()
Is there any way to directly convert the 2, 8 and 16 generations into 10 types?
java.lang.Integer class
parseInt(String s, int radix)
Use the base specified by the second parameter to parse the parameter into signed energy.
Examples of jdk:
parseInt("0", 10) returns 0
parseInt("473", 10) returns 473
parseInt("-0", 10) returns 0
parseInt("-FF", 16) returns -255
parseInt("1100110", 2) returns 102
parseInt("2147483647", 10) returns 2147483647
parseInt("-2147483648", 10) returns -2147483648
parseInt("2147483648", 10) throws NumberFormatException
parseInt("99", throws a NumberFormatException
parseInt("Kona", 10) throws NumberFormatException
parseInt("Kona", 27) returns 411787
How to write decimal conversion (two, eight, sixteen) without algorithm
Integer.toBinaryString
Integer.toOctalString
Integer.toHexString
Example two
Public class test {
Public static invalid main (string parameter []) {
International i = 100;
String binStr=Integer.toBinaryString(i);
String otcStr=Integer.toOctalString(i);
String hexStr=Integer.toHexString(i);
System.out.println(binStr);
}
Example two
Public class TestStringFormat {
Public static void main(string[] args){
If (args.length == 0) {
System.out.println("Usage: java TestStringFormat <a number>");
System.exit(0);
}
Integer factor = Integer.valueOf(args[0]);
String
s = String.format("%d", factor);
System.out.println(s);
s = String.format("%x", factor);
System.out.println(s);
s = String.format("%o", factor);
System.out.println(s);
}
}
Other methods:
Integer.toHexString (your decimal number);
E.g
String temp = Integer.toHexString(75);
The output temperature is 4b
//Enter a 10-digit number and convert it to a 16-digit number
Import java.io.*;
Public class toHex{
public static void main(String[]args){
int input;//Execute input data
//Create an instance of the input string
BufferedReader strin=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter a time:");
String x=null;
try{
x=strin.readLine();
}catch(IOException ex){
ex.printStackTrace();
}
Input=Integer.parseInt(x);
System.out.println("The number you input is: "+input);//output the number received from the input
System.out.println("Its hexadecimal is:"+Integer.toHexString(input));//Use toHexString to convert decimal to hexadecimal
}
}