* Creating a deep vs shallow copy of an object in Java * Creating a deep vs shallow copy of an object in Java * Baeldung article converting number bases * Baeldung article converting number bases * edits made to Converting a Number from One Base to Another in Java * added braces to oneliners * added precondition to check input
This commit is contained in:
parent
1b25127d89
commit
305b369f5d
|
@ -2,68 +2,76 @@ package com.baeldung.convertnumberbases;
|
|||
|
||||
public class ConvertNumberBases {
|
||||
|
||||
public static String convertNumberToNewBase(String number, int base, int newBase){
|
||||
public static String convertNumberToNewBase(String number, int base, int newBase) {
|
||||
return Integer.toString(Integer.parseInt(number, base), newBase);
|
||||
}
|
||||
|
||||
public static String convertNumberToNewBaseCustom(String num, int base, int newBase) {
|
||||
int decimalNumber = convertFromAnyBaseToDecimal(num, base);
|
||||
return convertFromDecimalToBaseX(decimalNumber, newBase);
|
||||
String targetBase = "";
|
||||
try {
|
||||
targetBase = convertFromDecimalToBaseX(decimalNumber, newBase);
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return targetBase;
|
||||
}
|
||||
|
||||
public static String convertFromDecimalToBaseX(int num, int newBase) {
|
||||
public static String convertFromDecimalToBaseX(int num, int newBase) throws IllegalArgumentException {
|
||||
if ((newBase < 2 || newBase > 10) && newBase != 16) {
|
||||
throw new IllegalArgumentException("New base must be from 2 - 10 or 16");
|
||||
}
|
||||
|
||||
String result = "";
|
||||
int remainder;
|
||||
while (num > 0) {
|
||||
remainder = num % newBase;
|
||||
if (newBase == 16) {
|
||||
if (remainder == 10)
|
||||
if (remainder == 10) {
|
||||
result += 'A';
|
||||
else if (remainder == 11)
|
||||
} else if (remainder == 11) {
|
||||
result += 'B';
|
||||
else if (remainder == 12)
|
||||
} else if (remainder == 12) {
|
||||
result += 'C';
|
||||
else if (remainder == 13)
|
||||
} else if (remainder == 13) {
|
||||
result += 'D';
|
||||
else if (remainder == 14)
|
||||
} else if (remainder == 14) {
|
||||
result += 'E';
|
||||
else if (remainder == 15)
|
||||
} else if (remainder == 15) {
|
||||
result += 'F';
|
||||
else
|
||||
} else {
|
||||
result += remainder;
|
||||
} else
|
||||
}
|
||||
} else {
|
||||
result += remainder;
|
||||
|
||||
}
|
||||
num /= newBase;
|
||||
}
|
||||
return new StringBuffer(result).reverse().toString();
|
||||
}
|
||||
|
||||
public static int convertFromAnyBaseToDecimal(String num, int base) {
|
||||
|
||||
if (base < 2 || (base > 10 && base != 16))
|
||||
if (base < 2 || (base > 10 && base != 16)) {
|
||||
return -1;
|
||||
|
||||
}
|
||||
int val = 0;
|
||||
int power = 1;
|
||||
|
||||
for (int i = num.length() - 1; i >= 0; i--) {
|
||||
int digit = charToDecimal(num.charAt(i));
|
||||
|
||||
if (digit < 0 || digit >= base)
|
||||
if (digit < 0 || digit >= base) {
|
||||
return -1;
|
||||
|
||||
}
|
||||
val += digit * power;
|
||||
power = power * base;
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
public static int charToDecimal(char c) {
|
||||
if (c >= '0' && c <= '9')
|
||||
if (c >= '0' && c <= '9') {
|
||||
return (int) c - '0';
|
||||
else
|
||||
} else {
|
||||
return (int) c - 'A' + 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.baeldung.convertnumberbases;
|
||||
|
||||
import static com.baeldung.convertnumberbases.ConvertNumberBases.convertFromDecimalToBaseX;
|
||||
import static com.baeldung.convertnumberbases.ConvertNumberBases.convertNumberToNewBase;
|
||||
import static com.baeldung.convertnumberbases.ConvertNumberBases.convertNumberToNewBaseCustom;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
@ -17,4 +18,15 @@ class ConvertNumberBasesUnitTest {
|
|||
assertEquals(convertNumberToNewBaseCustom("11001000", 2, 8), "310");
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenInputIsOutOfRange_thenIllegalArgumentExceptionIsThrown() {
|
||||
Exception exception = assertThrows(IllegalArgumentException.class, ()-> {
|
||||
convertFromDecimalToBaseX(100, 12);
|
||||
});
|
||||
String expectedMessage = "New base must be from 2 - 10 or 16";
|
||||
String actualMessage = exception.getMessage();
|
||||
|
||||
assertTrue(actualMessage.contains(expectedMessage));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue