[Java] binary, octal, hexadecimal, and decimal conversion
Decimal to hexadecimal: integer. tohexstring (int I) decimal to octal integer. tooctalstring (int I) decimal to binary integer. tobinarystring (int I) hexadecimal to decimal integer. valueof ("ffff", 16 ). tostring () octal to decimal integer. valueof ("876", 8 ). tostring () binary to decimal integer. valueof ("0101", 2 ). tostring () Is there any way to directly convert 2, 8, and 16 into 10? Java. Lang. Integer parseint (string S, int Radix) uses the base number specified by the second parameter to resolve the string parameter to a signed integer. Examples from 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 a numberformatexception parseint ("99", throws a numberformatexception parseint ("Kona ", 10) throws a numberformatexception parseint ("Kona", 27) how to write the returns 411787 hexadecimal conversion (2, 8, 16) without using an integer algorithm. tobinarystring integer. tooctalstring integer. tohexstring Example 2 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 2 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; 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); for example, string temp = integer. tohexstring (75); the output temp is 4B // enter a decimal number and convert it to hexadecimal import Java. io. *; public class tohex {public static void main (string [] ARGs) {int input; // store input data // create an instance of the input string bufferedreader strin = new bufferedreader (New inputstreamreader (system. in); system. out. println ("enter an integer:"); string x = NULL; try {x = strin. readline ();} catch (ioexception ex) {ex. printstacktrace ();} input = integer. parseint (x); system. out. println ("the number you entered is:" + input); // output the number system received from the keyboard. out. println ("its hexadecimal format is:" + integer. tohexstring (input); // use tohexstring to convert the hexadecimal value to the hexadecimal value }}