Binary, octal, and hexadecimal Conversions in Java

Source: Internet
Author: User
Tags decimal to binary hex to decimal

Convert from Binary, octal or Hex to Decimal using Integer.parseint (String input, int radix)

Use-to Integer.parseInt(String input, int radix) -convert from any type of Integer .

String binarynumber = "10101"; int decimal1 = Integer.parseint (Binarynumber, 2); System.out.println (Binarynumber + "in Base:" + decimal1); String Octalnumber = "456"; int decimal2 = Integer.parseint (Octalnumber, 8); System.out.println (Octalnumber + "in Base:" + decimal2); String hexnumber = "ABCD"; int decimal3 = Integer.parseint (Hexnumber, 16); System.out.println (Hexnumber + "in base:" + Decimal3), output:10101 in base 10:21456 in base 10:302ABCD in base 1 0:43,981
Convert from Decimal to Binary, octal or Hex using integer.tostring (int input, int radix)

Use-to-convert from a to any Integer.toString(int input, int radix) Integer type of base number.

Integer decimal1 = 21; String Binarynumber = integer.tostring (Decimal1, 2); System.out.println (Decimal1 + "in Base 2:" + binarynumber); Integer decimal2 = 302; String Octalnumber = integer.tostring (Decimal2, 8); System.out.println (Decimal2 + "in Base 8:" + octalnumber); Integer decimal3 = 43981; String Hexnumber = integer.tostring (Decimal3, 16);  System.out.println (Decimal2 + "in base:" + Hexnumber), output:21 in base 2:10101302 in base 8:45643981 in base 16 : ABCD
Convert from Decimal to Binary, octal or Hex using integer.toxxxstring (int)

IntegerClass does provide lots of unitily methods to be consumed directly. Check out the them in Java Doc.

Integer decimal1 = 21; System.out.println (Decimal1 + "in Base 2:" + integer.tobinarystring (decimal1)); Integer Decimal2 = 302; System.out.println (Decimal2 + "in Base 8:" + integer.tooctalstring (decimal2)); Integer decimal3 = 43981;  System.out.println (Decimal3 + "in base:" + integer.tohexstring (DECIMAL3)), output:21 in base 2:10101302 in base 8: 45643981 in Base 16:ABCD

Binary, octal, and hexadecimal Conversions in Java

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.