Example usage for java.math BigInteger toString

List of usage examples for java.math BigInteger toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns the decimal String representation of this BigInteger.

Usage

From source file:org.openmrs.module.casereport.DocumentUtil.java

/**
 * Converts the specified uuid to its decimal representation
 * //from  w ww.  jav a 2 s.  c  o m
 * @param uuid the uuid to convert
 * @return a string representation of the decimal number
 */
public static String convertToDecimal(UUID uuid) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());
    BigInteger bi = new BigInteger(bb.array());
    //Get the unsigned representation for -ve numbers
    if (bi.compareTo(BigInteger.ZERO) < 0) {
        bi = DECIMAL_REP_COUNT.add(bi);
    }
    return bi.toString();
}

From source file:edu.umn.msi.tropix.proteomics.conversion.impl.ConversionUtils.java

public static Integer getChargeState(final Scan scan) {
    Integer chargeState = null;// www .  ja va  2  s.c o m
    final List<Scan.PrecursorMz> precursorMzList = scan.getPrecursorMz();
    if (precursorMzList != null && precursorMzList.size() > 0) {
        final BigInteger precursorCharge = scan.getPrecursorMz().get(0).getPrecursorCharge();
        if (precursorCharge != null) {
            chargeState = Integer.parseInt(precursorCharge.toString());
        }
    }
    return chargeState;
}

From source file:main.java.utils.Utility.java

public static String asUnsignedDecimalString(long l) {
    /** the constant 2^64 */
    BigInteger TWO_64 = BigInteger.ONE.shiftLeft(64);
    BigInteger b = BigInteger.valueOf(l);

    if (b.signum() < 0) {
        b = b.add(TWO_64);/*from   www  .  j a v a2  s. c  o  m*/
    }

    return b.toString();
}

From source file:com.forsrc.utils.MyRsaUtils.java

/**
 * Encrypt 2 string.// ww w  . j a va 2s.  com
 *
 * @param rsaKey    the rsa key
 * @param plaintext the plaintext
 * @return the string
 */
public static String encrypt2(RsaKey rsaKey, String plaintext) {

    BigInteger[] plaintextBigIntegers = string2BigIntegers(plaintext);
    StringBuilder sb = new StringBuilder(BLOCK_SIZE + 1);
    for (BigInteger bigInteger : plaintextBigIntegers) {
        BigInteger encrypt = encrypt(rsaKey, bigInteger);
        sb.append(encrypt.toString()).append("$");
    }
    sb.delete(sb.length() - 1, sb.length());

    return new String(new Base64().encode(sb.toString().getBytes()));
}

From source file:com.forsrc.utils.MyRsaUtils.java

/**
 * Encrypt string.//from w  w w. j  a va2  s .  c  o m
 *
 * @param rsaKey    the rsa key
 * @param plaintext the plaintext
 * @return the string
 */
public static String encrypt(RsaKey rsaKey, String plaintext) {
    BigInteger plaintextNumber = string2BigInteger(plaintext);
    BigInteger encrypt = encrypt(rsaKey, plaintextNumber);
    return new String(new Base64().encode(encrypt.toString().getBytes()));
}

From source file:Bytes.java

/**
 * Convert the specified amount into a human readable (though slightly less accurate)
 * result. IE:/*from www  .  ja v a 2 s  .  c o  m*/
 * '4096 B' to '4 KB'
 * '5080 B' to '5 KB' even though it is really '4 KB + 984 B'
 */
public static String friendly(Bytes type, BigInteger value) {
    /**
     * Logic:
     * Loop from YB to B
     * If result = 0, continue
     * Else, round off
     *
     * NOTE: BigIntegers are not reusable, so not point in caching them outside the loop
     */
    for (Bytes newType : reversed) {
        BigInteger newAmount = newType.convertFrom(value, type);
        if (newAmount.equals(BigInteger.ZERO))
            continue;
        // Found the right one. Now to round off
        BigInteger unitBytes = Bytes.B.convertFrom(BigInteger.ONE, newType);
        BigInteger usedBytes = newAmount.multiply(unitBytes);
        BigInteger remainingBytes = Bytes.B.convertFrom(value, type).subtract(usedBytes);
        if (remainingBytes.equals(BigInteger.ZERO))
            return String.format(friendlyFMT, newAmount.toString(), newType);
        if (remainingBytes.equals(value))
            return String.format(friendlyFMT, newAmount.toString(), newType);

        BigInteger halfUnit = unitBytes.divide(TWO);
        if ((remainingBytes.subtract(halfUnit)).signum() < 0)
            return String.format(friendlyFMT, newAmount.toString(), newType);

        return String.format(friendlyFMT, (newAmount.add(BigInteger.ONE)).toString(), newType);
    }

    // Give up
    return String.format(friendlyFMT, value.toString(), type);
}

From source file:org.opendaylight.genius.interfacemanager.renderer.ovs.utilities.SouthboundUtils.java

public static String generateOfTunnelName(BigInteger dpId, IfTunnel ifTunnel) {
    String sourceKey = new String(ifTunnel.getTunnelSource().getValue());
    String remoteKey = new String(ifTunnel.getTunnelDestination().getValue());
    if (ifTunnel.isTunnelSourceIpFlow() != null) {
        sourceKey = "flow";
    }//w ww. j a  va2s .com
    if (ifTunnel.isTunnelRemoteIpFlow() != null) {
        remoteKey = "flow";
    }
    String tunnelNameKey = dpId.toString() + sourceKey + remoteKey;
    String uuidStr = UUID.nameUUIDFromBytes(tunnelNameKey.getBytes()).toString().substring(0, 12).replace("-",
            "");
    return String.format("%s%s", "tun", uuidStr);
}

From source file:petascope.util.ras.RasUtil.java

/**
 * Deletes an array from rasdaman./*from  ww w.java2  s .c o m*/
 *
 * @param oid
 * @param collectionName
 * @throws RasdamanException
 */
public static void deleteFromRasdaman(BigInteger oid, String collectionName) throws RasdamanException {
    String query = TEMPLATE_DELETE.replaceAll(TOKEN_COLLECTION_NAME, collectionName).replace(TOKEN_OID,
            oid.toString());
    executeRasqlQuery(query, ConfigManager.RASDAMAN_ADMIN_USER, ConfigManager.RASDAMAN_ADMIN_PASS, true);
    //check if there are other objects left in the collection
    log.info("Checking the number of objects left in collection " + collectionName);
    RasBag result = (RasBag) executeRasqlQuery(TEMPLATE_SDOM.replace(TOKEN_COLLECTION_NAME, collectionName));
    log.info("Result size is: " + String.valueOf(result.size()));
    if (result.size() == 0) {
        //no object left, delete the collection so that the name can be reused in the future
        log.info(
                "No objects left in the collection, dropping the collection so the name can be reused in the future.");
        executeRasqlQuery(TEMPLATE_DROP_COLLECTION.replace(TOKEN_COLLECTION_NAME, collectionName),
                ConfigManager.RASDAMAN_ADMIN_USER, ConfigManager.RASDAMAN_ADMIN_PASS, true);
    }
}

From source file:com.forsrc.utils.MyRsaUtils.java

private static String toStr(BigInteger number) throws IOException {

    StringBuilder plaintext = new StringBuilder(BLOCK_SIZE + 1);

    String plaintextNumber = number.toString();

    plaintextNumber = plaintextNumber.substring(1, plaintextNumber.length());
    for (int i = 0; i < plaintextNumber.length(); i += 3) {
        String blockString = plaintextNumber.substring(i, i + 3);
        int block = Integer.parseInt(blockString);
        plaintext.append((char) block);
    }//from   w  w  w . j  av  a 2  s  .c om

    return plaintext.toString();
}

From source file:org.alfresco.mobile.android.api.network.NetworkHttpInvoker.java

private static Response invoke(UrlBuilder url, String method, String contentType,
        Map<String, List<String>> httpHeaders, Output writer, boolean forceOutput, BigInteger offset,
        BigInteger length, Map<String, String> params) {
    try {/*  w w  w .  j  av  a2  s.c o  m*/
        // Log.d("URL", url.toString());

        // connect
        HttpURLConnection conn = (HttpURLConnection) (new URL(url.toString())).openConnection();
        conn.setRequestMethod(method);
        conn.setDoInput(true);
        conn.setDoOutput(writer != null || forceOutput);
        conn.setAllowUserInteraction(false);
        conn.setUseCaches(false);
        conn.setRequestProperty("User-Agent", ClientVersion.OPENCMIS_CLIENT);

        // set content type
        if (contentType != null) {
            conn.setRequestProperty("Content-Type", contentType);
        }
        // set other headers
        if (httpHeaders != null) {
            for (Map.Entry<String, List<String>> header : httpHeaders.entrySet()) {
                if (header.getValue() != null) {
                    for (String value : header.getValue()) {
                        conn.addRequestProperty(header.getKey(), value);
                    }
                }
            }
        }

        // range
        BigInteger tmpOffset = offset;
        if ((tmpOffset != null) || (length != null)) {
            StringBuilder sb = new StringBuilder("bytes=");

            if ((tmpOffset == null) || (tmpOffset.signum() == -1)) {
                tmpOffset = BigInteger.ZERO;
            }

            sb.append(tmpOffset.toString());
            sb.append("-");

            if ((length != null) && (length.signum() == 1)) {
                sb.append(tmpOffset.add(length.subtract(BigInteger.ONE)).toString());
            }

            conn.setRequestProperty("Range", sb.toString());
        }

        conn.setRequestProperty("Accept-Encoding", "gzip,deflate");

        // add url form parameters
        if (params != null) {
            DataOutputStream ostream = null;
            OutputStream os = null;
            try {
                os = conn.getOutputStream();
                ostream = new DataOutputStream(os);

                Set<String> parameters = params.keySet();
                StringBuffer buf = new StringBuffer();

                int paramCount = 0;
                for (String it : parameters) {
                    String parameterName = it;
                    String parameterValue = (String) params.get(parameterName);

                    if (parameterValue != null) {
                        parameterValue = URLEncoder.encode(parameterValue, "UTF-8");
                        if (paramCount > 0) {
                            buf.append("&");
                        }
                        buf.append(parameterName);
                        buf.append("=");
                        buf.append(parameterValue);
                        ++paramCount;
                    }
                }
                ostream.writeBytes(buf.toString());
            } finally {
                if (ostream != null) {
                    ostream.flush();
                    ostream.close();
                }
                IOUtils.closeStream(os);
            }
        }

        // send data

        if (writer != null) {
            // conn.setChunkedStreamingMode((64 * 1024) - 1);
            OutputStream connOut = null;
            connOut = conn.getOutputStream();
            OutputStream out = new BufferedOutputStream(connOut, BUFFER_SIZE);
            writer.write(out);
            out.flush();
        }

        // connect
        conn.connect();

        // get stream, if present
        int respCode = conn.getResponseCode();
        InputStream inputStream = null;
        if ((respCode == HttpStatus.SC_OK) || (respCode == HttpStatus.SC_CREATED)
                || (respCode == HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION)
                || (respCode == HttpStatus.SC_PARTIAL_CONTENT)) {
            inputStream = conn.getInputStream();
        }

        // get the response
        return new Response(respCode, conn.getResponseMessage(), conn.getHeaderFields(), inputStream,
                conn.getErrorStream());
    } catch (Exception e) {
        throw new CmisConnectionException("Cannot access " + url + ": " + e.getMessage(), e);
    }
}