Convert the java decimal number (negative number supported) to the n-base (n is between [2, 36)

Source: Internet
Author: User

Convert the java decimal number (negative number supported) to the n-base (n is between [2, 36)

Public class DecimalTONBigit {/*** decimal number (which can be a negative number) to n (n is between [2, 36) hexadecimal * @ author wl */public static final int N = 36; // N indicates hexadecimal public static void main (String [] args) {int data = 71; // data indicates the number of toNBigit (data, N) to be converted;} private static void toNBigit (int data, int a) {if (a> 36 | a <2) {System. out. println ("This hexadecimal format is not supported !!! "); Return;} if (data <0) {data = (-1 * data) ^ (1 <31)-1) + 1; // change the negative value to a positive value, and then return the inverse value (^ (1 <31)-1). Then, add 1 ;} int n = (data % a); int m = (data/a); if (m = 0) {printNBigitNum (n);} else {toNBigit (m, a); printNBigitNum (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 hexadecimal format is not supported !!! "); Return;} else {System. out. print (n );}}}

Output:

1Z

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.