[JAVA] binary, octal, hexadecimal, decimal to convert between __java

Source: Internet
Author: User
Tags binary to decimal
[JAVA] binary, octal, hexadecimal, decimal between conversions
Decimal turns into 16: integer.tohexstring (int i) decimal into octal integer.tooctalstring (int i) decimal into binary integer.tobinarystring (int i) hexadecimal to decimal integer.valueof ("FFFF"). ToString () octal to decimal integer.valueof ("876", 8). ToString () binary to decimal intege 

R.valueof ("0101", 2). ToString () is there any way to directly convert the 2,8,16 into a 10-in-system? 

The Java.lang.Integer class parseint (string s, int radix) resolves a string parameter to a signed integer using the cardinality specified by the second argument. Examples from Jdk:parseint ("0") returns 0 parseint ("473", Ten) returns 473 ("0",) parseint 0 returns NT ("-ff") returns-255 parseint ("1100110", 2) returns 102 ("parseint",) 2147483647 returns  (" -2147483648", returns-2147483648 parseint ("2147483648",) throws a numberformatexception parseint ("the", throws A numberformatexception parseint ("Kona", throws a NumberFormatException parseint ("Kona",) returns 411787 How to write conversion (two, eight, 16) without algorithm integer.tobinarystring integer.tooctalstring integer.tohexstring case Two public class test{public static void Main (String args[]) {int 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) {Syst 

Em.out.println ("Usage:java teststringformat <a number>"); 

System.exit (0); 



Integer factor = integer.valueof (Args[0]); 



String s; 

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 10 number); 

For example String temp = integer.tohexstring (75); 

The output TEMP is 4b//input a 10 binary number and converts it into a 16 import java.io.*; public class tohex{public static void Main (String[]args) {int input;//holds an instance of input data//create an input string BufferedReader stri N=new BufferedReader (New InputstreAmreader (system.in)); 

System.out.println ("Please enter an integer:"); 

String X=null; 

try{X=strin.readline (); 

}catch (IOException ex) {ex.printstacktrace (); 

} input=integer.parseint (x); System.out.println ("You enter the number is:" +input)//output from the keyboard received the number System.out.println ("Its 16 is:" +integer.tohexstring (input)); 

 Use tohexstring to convert 10 into 16}}
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.