Binary conversion Issues in Java

Source: Internet
Author: User

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 ("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

Arbitrary-binary conversions

Import Java.util.HashMap;  Import Java.util.Map;  Import Java.util.Stack; public class Hextransformatutil {/** * converts decimal to any binary value * @param the converted binary minimum bit, followed by a character value, such as 26 binary "Abcdefghi JKLMNOPQRSTUVWXYZ "* @param num Converts the decimal value * @param length to the specified string, if insufficient length length, automatically complements the minimum value, such as 26 binary" Abcdefghijklmno            PQRSTUVWXYZ "a" * @return */public static string Hex10toanly (string digths, int num,int length) {            StringBuffer str = new StringBuffer ("");          int base = Digths.trim (). Length ();          if (0==num) {str.append (Digths.charat (0));                }else{stack<character> s = new stack<character> ();                    while (num! = 0) {s.push (Digths.charat (num%base));                Num/=base;                } while (!s.isempty ()) {Str.append (S.pop ());          }} String prefix = "";          String suffix = str.tostring (); IF (Length>suffix.length ()) {for (int count = 0;count<length-suffix.length (); count++) {PR              Efix = prefix + digths.charat (0);        }} return prefix + suffix; /** * Convert any binary to a decimal value * @param digths the binary minimum bit before conversion, the character value that appears sequentially, such as 26 binary "ABCDEFGHIJKLMNOPQRSTUVWXYZ" *            @param hexvalue binary String value before conversion * @return */public static int HexAnlyTo10 (string digths, String hexvalue) { if (null==hexvalue| | "".          Equals (Hexvalue.trim ())) return 0;                    int base = Digths.trim (). Length ();          map<string,integer> Digthmap = new hashmap<string,integer> ();          int count=0;              for (char Item:digths.trim (). ToCharArray ()) {Digthmap.put ("" +item, Count);          count++;          } String str = new StringBuffer (Hexvalue.trim ()). Reverse (). toString ();          int sum = 0; for (int index = 0;index<str.length (); index++) {sum = nEW Double (Math.pow (base, Index)). Intvalue () * Digthmap.get ("" +str.charat (index)) +sum;      } return sum; } public static void Main (string[] args) {//Converts the decimal "0123456789" value 55 to 26 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" pair                    The expected value does not require formatting of the last output string System.out.println (Hex10toanly ("abcdefghijklmnopqrstuvwxyz", 55, 0)); Converts the 26 binary "ABCDEFGHIJKLMNOPQRSTUVWXYZ" string value "CD" to the value corresponding to the decimal "0123456789" value 55, without having to format the last output string System.out.println (      HexAnlyTo10 ("abcdefghijklmnopqrstuvwxyz", "CD"));     }  }

  

Binary conversion Issues in Java

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.