"Four Java Learning notes" Java Binary conversion

Source: Internet
Author: User

Tag: binary 16 binary images row signed valueof [] static conversion

Decimal turns into 16 binary:
integer.tohexstring (int i)
Decimal into octal
integer.tooctalstring (int i)
Decimal turns into binary
integer.tobinarystring (int i)
Hex turns into decimal
Integer.valueof ("FFFF", +). ToString ()
Octal into decimal
Integer.valueof ("876", 8). ToString ()
Binary goto Decimal
Integer.valueof ("0101", 2). ToString ()

Is there any way to directly convert the 2,8,16 into a 10-binary system?
Java.lang.Integer class
parseint (String s, int radix)
Resolves a string argument to a signed integer using the cardinality specified by the second argument.
Examples from JDK:
parseint ("0", ten) returns 0
parseint ("473", Ten) returns 473
parseint ("-0", returns 0)
parseint ("-ff", +) returns-255
parseint ("1100110", 2) returns 102
parseint ("2147483647", ten) returns 2147483647
parseint (" -2147483648", ten) returns-2147483648
parseint ("2147483648", ten) throws a NumberFormatException
parseint ("throws", a numberformatexception
parseint ("Kona", ten) throws a NumberFormatException
parseint ("Kona", returns 411787)

How the binary conversion is written (two, eight, 16) without algorithms
Integer.tobinarystring
Integer.tooctalstring
Integer.tohexstring


Example 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) {
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 10 binary number);
For example
String temp = integer.tohexstring (75);
Output TEMP is 4b



Enter a 10 binary number and convert it to 16 binary
Import java.io.*;
public class tohex{

public static void Main (String[]args) {

int input;//Store input data
Creating an instance of an input string
BufferedReader strin=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 ("The number you entered is:" +input);//Output the number received from the keyboard


}
}

"Four Java Learning notes" Java Binary conversion

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.