Binary, octal, hexadecimal conversion

Source: Internet
Author: User
Tags binary to decimal 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 ()
Octal to decimal
Integer. valueof ("876", 8). tostring ()
Convert binary to decimal
Integer. valueof ("0101", 2). tostring ()

Is there any way to directly convert 2, 8, and 16 to 10?
Java. Lang. Integer class
Parseint (string S, int Radix)
Use 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) returns 411787.

How to Write (2, 8, 16) in hexadecimal conversion without Algorithms
Integer. 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 into a hexadecimal number.
Import java. Io .*;
Public class tohex {

Public static void main (string [] ARGs ){

Int input; // stores 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 received from the keyboard

System. Out. println ("its hexadecimal format is:" + integer. tohexstring (input); // use tohexstring to convert the hexadecimal value to the hexadecimal value.
}
}

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.