Merge pull request #10917 from amitiw4u/BAEL-4960-StringToBigInt
Bael 4960 string to big int
This commit is contained in:
commit
e056bc4f34
|
@ -0,0 +1,44 @@
|
||||||
|
package com.baeldung.stringtobybiginteger;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class StringToBigIntegerUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenGetStringWithOutRadix_thenOK() {
|
||||||
|
String inputString = "878";
|
||||||
|
BigInteger result = new BigInteger(inputString);
|
||||||
|
assertEquals("878", result.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenGetStringWithRadix_thenOK() {
|
||||||
|
String inputString = "290f98";
|
||||||
|
BigInteger result = new BigInteger(inputString, 16);
|
||||||
|
assertEquals("2690968", result.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = NumberFormatException.class)
|
||||||
|
public void whenGetStringWithOutRadix_thenThrowError() {
|
||||||
|
String inputString = "290f98";
|
||||||
|
new BigInteger(inputString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = NumberFormatException.class)
|
||||||
|
public void whenGetStringWithRadix_thenThrowError() {
|
||||||
|
String inputString = "290f98";
|
||||||
|
new BigInteger(inputString, 7);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenGetStringUsingByte_thenOk() {
|
||||||
|
String inputString = "290f98";
|
||||||
|
byte[] inputStringBytes = inputString.getBytes();
|
||||||
|
BigInteger result = new BigInteger(inputStringBytes);
|
||||||
|
assertEquals("290f98", new String(result.toByteArray()));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue