Java decimal number (supports negative numbers) to n binary (n between [2,36])

Source: Internet
Author: User

public class Decimaltonbigit {/** * decimal number (can be negative) to N (n between [2,36]) * @author wl */public static final int n=36;//n represents N binary public static void Main (string[] args) {int Data=71;//data represents the number of Tonbigit (data,n) to convert;} private static void Tonbigit (int data, int a) {if (a>36| | A<2) {System.out.println ("This binary is not supported!!! "); return;} if (data<0) {data= (( -1*data) ^ ((1<<31)-1)) +1;//turns negative numbers to positive numbers, then negation (^ ((1<<31)-1) is reversed) and finally 1;}int n= (data%a); int m= (data/a); if (m==0) {printnbigitnum (n);} Else{tonbigit (m,a);p rintnbigitnum (n);}} private static void Printnbigitnum (int n) {if (n>9&&n<=36) {System.out.print ((char) ((n-10) +65));} else if (n>36) {System.out.println ("This binary is not supported!!! "); return;} Else{system.out.print (n);}}

Output:

1Z

Java decimal number (supports negative numbers) to n binary (n between [2,36])

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.