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