1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| public class ConversionBase {
public static String convertJava(String number, int from, int to) {
return Integer.toString(Integer.parseInt(number, from),to);
}
public static String convert(String number, int from, int to) {
Objects.requireNonNull(number,"Number must be not null");
return convert(convert(number, from),to);
}
public static int convert(String number, int radix) {
Objects.requireNonNull(number,"Number must be not null");
if ( radix<Character.MIN_RADIX || radix>Character.MAX_RADIX ) {
throw new NumberFormatException("Illegal radix: " + radix);
}
boolean minus=false;
if ( number.trim().isEmpty() ) {
throw new NumberFormatException("For input string: \"" + number + "\"");
}
else if ( number.startsWith("-")) {
minus = true;
number = number.substring(1);
}
int result=0;
int pow=1;
final char[] chars=number.toCharArray();
for(int i=chars.length-1; i>=0; i--) {
if (pow<=0) throw new NumberFormatException("For input string: \"" + number + "\"");
final int value = valueOf(number, chars[i]);
if ( value<radix ) {
result+=value*pow;
}
else {
throw new NumberFormatException("For input string: \"" + number + "\"");
}
pow*=radix;
}
return minus?-result:result;
}
private final static int MAX_DIGIT_10=(int)Math.log(Integer.MAX_VALUE)+1;
public static String convert(int number, int radix) {
if ( radix<Character.MIN_RADIX || radix>Character.MAX_RADIX ) {
throw new NumberFormatException("Illegal radix: " + radix);
}
boolean minus=false;
if ( number==0 ) {
return "0";
}
else if ( number<0 ) {
minus=true;
number=-number;
}
final StringBuilder stringBuilder=new StringBuilder(MAX_DIGIT_10+(minus?1:0));
while(number>0) {
stringBuilder.insert(0, valueOf(number%radix));
number/=radix;
}
if ( minus ) {
stringBuilder.insert(0,'-');
}
return stringBuilder.toString();
}
private static int valueOf(String number, char c) {
if ( '0'<=c && c<='9') {
return c-'0';
}
else {
c = Character.toLowerCase(c);
if ( c>='a' && c<='z' ) {
return 10+c-'a';
}
else {
throw new NumberFormatException("For input string: \"" + number + "\"");
}
}
}
private static char valueOf(int c) {
if ( 0<=c && c<=9) {
return (char)('0'+c);
}
else {
return (char)('a'+(c-10));
}
}
public static void main(String[] args) {
test("123",10,2);
test("123",8,2);
test("123",10,10);
test("123",4,10);
test("1111111111111111111111111111111",2,10);
test("FF01",16,2);
test("abcd",16,10);
test("abcd",20,10);
test("-42",10,4);
test("-42",10,2);
}
private static void test(String number, int from, int to) {
System.out.println("Convert "+number+" from " + from + " to " + to +":");
System.out.println("Java: " + convertJava(number, from, to));
System.out.println("Self: " + convert(number, from, to));
}
} |
Partager