Example usage for java.math BigInteger valueOf

List of usage examples for java.math BigInteger valueOf

Introduction

In this page you can find the example usage for java.math BigInteger valueOf.

Prototype

private static BigInteger valueOf(int val[]) 

Source Link

Document

Returns a BigInteger with the given two's complement representation.

Usage

From source file:eu.operando.operandoapp.wifi.model.WiFiUtils.java

public static String convertIpAddress(int ipAddress) {
    byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray();
    ArrayUtils.reverse(bytes);//  w  w w .j  a v  a2  s.  c o  m
    try {
        return InetAddress.getByAddress(bytes).getHostAddress();
    } catch (UnknownHostException e) {
        return StringUtils.EMPTY;
    }
}

From source file:MainClass.java

public static X509Certificate generateV1Certificate(KeyPair pair)
        throws InvalidKeyException, NoSuchProviderException, SignatureException {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();

    certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
    certGen.setIssuerDN(new X500Principal("CN=Test Certificate"));
    certGen.setNotBefore(new Date(System.currentTimeMillis() - 10000));
    certGen.setNotAfter(new Date(System.currentTimeMillis() + 10000));
    certGen.setSubjectDN(new X500Principal("CN=Test Certificate"));
    certGen.setPublicKey(pair.getPublic());
    certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");

    return certGen.generateX509Certificate(pair.getPrivate(), "BC");
}

From source file:cherry.foundation.crypto.SecureBigIntegerEncoderTest.java

@Test
public void testEncodeDecode() throws Exception {
    SecureBigIntegerEncoder encoder = createSecureBigIntegerEncoder();
    for (int i = 0; i < 100; i++) {
        BigInteger plain = BigInteger.valueOf(random.nextLong());
        String crypto = encoder.encode(plain);
        assertThat(crypto, is(not(plain.toString())));
        assertThat(encoder.decode(crypto), is(plain));
    }//from w  w w.j av a  2  s.c om
}

From source file:com.spotify.hdfs2cass.CassandraPartitionerTest.java

@Test
public void testGetPartition() throws Exception {
    final int maxNodes = 5;

    final List<String> tokenRanges = new ArrayList<String>();

    BigInteger start = BigInteger.ZERO;
    BigInteger step = RandomPartitioner.MAXIMUM.divide(BigInteger.valueOf(maxNodes));
    for (int i = 0; i < maxNodes - 1; i++) {
        BigInteger end = start.add(step);

        tokenRanges.add(String.format("%d:%d", start, end));
        start = end.add(BigInteger.ONE);
    }// w  ww  .  j  a  va 2s .c o  m

    tokenRanges.add(String.format("%d:0", start));

    final JobConf conf = new JobConf();
    conf.set(ClusterInfo.SPOTIFY_CASSANDRA_TOKENS_PARAM, StringUtils.join(tokenRanges, ","));
    conf.set(ClusterInfo.SPOTIFY_CASSANDRA_PARTITIONER_PARAM, "org.apache.cassandra.dht.RandomPartitioner");

    CassandraPartitioner instance = new CassandraPartitioner();
    instance.configure(conf);

    Text key = new Text("foobar");
    assertEquals(2, instance.getPartition(key, null, 5));

    key = new Text("someotherkey");
    assertEquals(1, instance.getPartition(key, null, 5));

    key = new Text("1ce5cf4b861941f4aa799ae39ac9daa4");
    assertEquals(4, instance.getPartition(key, null, 5));
}

From source file:co.rsk.peg.BridgeSerializationUtils.java

public static byte[] serializePairMap(SortedMap<Sha3Hash, Pair<BtcTransaction, Long>> map) {
    int ntxs = map.size();

    byte[][] bytes = new byte[ntxs * 3][];
    int n = 0;/* w  w  w. ja  v a  2s. c om*/

    for (Map.Entry<Sha3Hash, Pair<BtcTransaction, Long>> entry : map.entrySet()) {
        bytes[n++] = RLP.encodeElement(entry.getKey().getBytes());
        bytes[n++] = RLP.encodeElement(entry.getValue().getLeft().bitcoinSerialize());
        bytes[n++] = RLP.encodeBigInteger(BigInteger.valueOf(entry.getValue().getRight()));
    }

    return RLP.encodeList(bytes);
}

From source file:com.netflix.imfutility.ConversionHelperTest.java

@Test
public void editUnitsToTimecode() {
    assertEquals("01:23:35.080",
            ConversionHelper.editUnitToTimecode(BigInteger.valueOf(125377), new BigFraction(25)));
    assertEquals("00:00:00.000",
            ConversionHelper.editUnitToTimecode(BigInteger.valueOf(0), new BigFraction(25)));
    assertEquals("00:00:02.000",
            ConversionHelper.editUnitToTimecode(BigInteger.valueOf(100), new BigFraction(50)));
    assertEquals("00:00:00.200",
            ConversionHelper.editUnitToTimecode(BigInteger.valueOf(10), new BigFraction(50)));
}

From source file:jp.co.ntts.vhut.util.MacConversionUtil.java

/**
 * ?????????.//w  w w .j a va2  s.c om
 * @param macStart .
 * @param count ?
 * @return 
 */
public static byte[] getMacAddressWithOrderAsByte(String macStart, int count) {
    BigInteger biMacStart = new BigInteger(addrToByte(macStart));
    BigInteger biSub = BigInteger.valueOf(count);
    byte[] bMacTarget = biMacStart.add(biSub).toByteArray();
    byte[] result = new byte[6];
    int delta = 6 - bMacTarget.length;
    for (int i = 0; i < 6; i++) {
        if (i >= delta) {
            result[i] = bMacTarget[i - delta];
        } else {
            result[i] = (byte) 0;
        }
    }
    return result;
}

From source file:cherry.foundation.type.converter.SecureBigIntegerConverterTest.java

@Test
public void testConvert() {
    for (int i = 0; i < 100; i++) {
        BigInteger plain = BigInteger.valueOf(random.nextLong());
        String crypto = SecureBigInteger.plainValueOf(plain).crypto();
        assertThat(cs.convert(crypto, SecureBigInteger.class).plain(), is(plain));
    }//from   w ww  .  j  av a 2s  . c o  m
}

From source file:NumberUtils.java

/**
 * Convert the given number into an instance of the given target class.
 *
 * @param number      the number to convert
 * @param targetClass the target class to convert to
 * @return the converted number/*ww w  . j a v  a2 s. c o  m*/
 * @throws IllegalArgumentException if the target class is not supported
 *                                  (i.e. not a standard Number subclass as included in the JDK)
 * @see java.lang.Byte
 * @see java.lang.Short
 * @see java.lang.Integer
 * @see java.lang.Long
 * @see java.math.BigInteger
 * @see java.lang.Float
 * @see java.lang.Double
 * @see java.math.BigDecimal
 */
public static Number convertNumberToTargetClass(Number number, Class targetClass)
        throws IllegalArgumentException {

    if (targetClass.isInstance(number)) {
        return number;
    } else if (targetClass.equals(Byte.class)) {
        long value = number.longValue();
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return number.byteValue();
    } else if (targetClass.equals(Short.class)) {
        long value = number.longValue();
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return number.shortValue();
    } else if (targetClass.equals(Integer.class)) {
        long value = number.longValue();
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return number.intValue();
    } else if (targetClass.equals(Long.class)) {
        return number.longValue();
    } else if (targetClass.equals(Float.class)) {
        return number.floatValue();
    } else if (targetClass.equals(Double.class)) {
        return number.doubleValue();
    } else if (targetClass.equals(BigInteger.class)) {
        return BigInteger.valueOf(number.longValue());
    } else if (targetClass.equals(BigDecimal.class)) {
        // using BigDecimal(String) here, to avoid unpredictability of BigDecimal(double)
        // (see BigDecimal javadoc for details)
        return new BigDecimal(number.toString());
    } else {
        throw new IllegalArgumentException("Could not convert number [" + number + "] of type ["
                + number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]");
    }
}

From source file:cc.redberry.core.number.Exponentiation.java

static BigInteger findIntegerRoot(BigInteger base, BigInteger power) {
    BigInteger maxBits = BigInteger.valueOf(base.bitLength() + 1); // base < 2 ^ (maxBits + 1)
    // => base ^ ( 1 / power ) < 2 ^ ( (maxBits + 1) / power )

    BigInteger[] divResult = maxBits.divideAndRemainder(power);
    if (divResult[1].signum() == 0) // i.e. divResult[1] == 0
        maxBits = divResult[0];/*  ww  w  . j  a v a 2 s . com*/
    else
        maxBits = divResult[0].add(BigInteger.ONE);

    if (maxBits.bitLength() > 31)
        throw new RuntimeException("Too many bits...");

    int targetBitsNumber = maxBits.intValue();
    int resultLengthM1 = targetBitsNumber / 8 + 1; //resultLength minus one
    byte[] result = new byte[resultLengthM1];
    resultLengthM1--;

    int bitNumber = targetBitsNumber;

    int cValue;
    BigInteger testValue;

    while ((--bitNumber) >= 0) {
        //setting bit
        result[resultLengthM1 - (bitNumber >> 3)] |= 1 << (bitNumber & 0x7);

        //Testing
        testValue = new BigInteger(result);
        cValue = ArithmeticUtils.pow(testValue, power).compareTo(base);
        if (cValue == 0)
            return testValue;
        if (cValue > 0)
            result[resultLengthM1 - (bitNumber >> 3)] &= ~(1 << (bitNumber & 0x7));
    }

    return null;
}