In Java, binary, octal, and hexadecimal are converted to each other in decimal format.

Source: Internet
Author: User
Tags decimal to binary
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
  
}
  
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.