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(int radix) 

Source Link

Document

Returns the String representation of this BigInteger in the given radix.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BigInteger bi = new BigInteger("1023");
    // Parse and format to binary
    bi = new BigInteger("1111111111", 2);
    String s = bi.toString(2);
    System.out.println(s);/*w w  w  .ja  va 2  s . co  m*/
    bi = new BigInteger("1000", 8);

    System.out.println(s = bi.toString(8));

    bi = new BigInteger("1023");
    s = bi.toString();
    System.out.println(s);

    bi = new BigInteger("3ff", 16);
    s = bi.toString(16);
    System.out.println(s);
}

From source file:BigNumApp.java

public static void main(String args[]) {
    BigInteger n = new BigInteger("1000000000000");
    BigInteger one = new BigInteger("1");
    while (!n.isProbablePrime(7))
        n = n.add(one);//w w w  . java  2  s . c o m
    System.out.println(n.toString(10) + " is probably prime.");
    System.out.println("It is " + n.bitLength() + " bits in length.");
}

From source file:dhtaccess.tools.Get.java

public static void main(String[] args) {
    boolean details = false;

    // parse properties
    Properties prop = System.getProperties();
    String gateway = prop.getProperty("dhtaccess.gateway");
    if (gateway == null || gateway.length() <= 0) {
        gateway = DEFAULT_GATEWAY;//from  w  ww.j  a va 2 s.  co  m
    }

    // parse options
    Options options = new Options();
    options.addOption("h", "help", false, "print help");
    options.addOption("g", "gateway", true, "gateway URI, list at http://opendht.org/servers.txt");
    options.addOption("d", "details", false, "print secret hash and TTL");

    CommandLineParser parser = new PosixParser();
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        System.out.println("There is an invalid option.");
        e.printStackTrace();
        System.exit(1);
    }

    String optVal;
    if (cmd.hasOption('h')) {
        usage(COMMAND);
        System.exit(1);
    }
    optVal = cmd.getOptionValue('g');
    if (optVal != null) {
        gateway = optVal;
    }
    if (cmd.hasOption('d')) {
        details = true;
    }

    args = cmd.getArgs();

    // parse arguments
    if (args.length < 1) {
        usage(COMMAND);
        System.exit(1);
    }

    // prepare for RPC
    DHTAccessor accessor = null;
    try {
        accessor = new DHTAccessor(gateway);
    } catch (MalformedURLException e) {
        e.printStackTrace();
        System.exit(1);
    }

    for (int index = 0; index < args.length; index++) {
        byte[] key = null;
        try {
            key = args[index].getBytes(ENCODE);
        } catch (UnsupportedEncodingException e1) {
            // NOTREACHED
        }

        // RPC
        if (args.length > 1) {
            System.out.println(args[index] + ":");
        }

        if (details) {
            Set<DetailedGetResult> results = accessor.getDetails(key);

            for (DetailedGetResult r : results) {
                String valString = null;
                try {
                    valString = new String((byte[]) r.getValue(), ENCODE);
                } catch (UnsupportedEncodingException e) {
                    // NOTREACHED
                }

                BigInteger hashedSecure = new BigInteger(1, (byte[]) r.getHashedSecret());

                System.out.println(valString + " " + r.getTTL() + " " + r.getHashType() + " 0x"
                        + ("0000000" + hashedSecure.toString(16)).substring(0, 8));
            }
        } else {
            Set<byte[]> results = accessor.get(key);

            for (byte[] valBytes : results) {
                try {
                    System.out.println(new String((byte[]) valBytes, ENCODE));
                } catch (UnsupportedEncodingException e) {
                    // NOTREACHED
                }
            }
        }
    } // for (int index = 0...
}

From source file:Main.java

/**
 * @param datapathId/*from www  . ja  va2  s .  c  o  m*/
 * @return readable version of datapathId (hex)
 */
public static String dumpDataPathId(BigInteger datapathId) {
    return datapathId.toString(16);
}

From source file:Main.java

public static String genRandomString(int length) {
    BigInteger bi = new BigInteger(130, random);
    return bi.toString(32);
}

From source file:Main.java

public static String hashImgUrl(String imgUrl) throws NoSuchAlgorithmException {
    String imgKey = null;/*  ww  w  . ja  v a  2s .co m*/
    MessageDigest m = MessageDigest.getInstance("MD5");
    m.reset();
    m.update(imgUrl.getBytes());
    byte[] digest = m.digest();
    BigInteger bigInt = new BigInteger(1, digest);
    imgKey = bigInt.toString(16);
    while (imgKey.length() < 32)
        imgKey = "0" + imgKey;
    return imgKey;
}

From source file:Main.java

public static String getStringMD5(String str) {
    String value = null;/*from  w  w  w  .  j  a  v  a 2  s.c  o  m*/
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(str.getBytes());
        BigInteger bi = new BigInteger(1, md5.digest());
        value = bi.toString(16).toUpperCase();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return value;
}

From source file:Main.java

/**
 * Convert a big integer into hex string. If the length is not even, add an
 * '0' character in the beginning to make it even.
 *//*  w w  w  .  ja v  a2s. c  o m*/
public static String toEvenLengthHex(BigInteger value) {
    String result = value.toString(16);
    if (result.length() % 2 != 0) {
        result = "0" + result;
    }
    return result;
}

From source file:Main.java

public static String md5(String senha) {
    String sen = "";
    MessageDigest md = null;/*from   ww w.j a va2s  .  c  om*/
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        Log.w("Problemas ao gerar MD5", e);
    }
    BigInteger hash = new BigInteger(1, md.digest(senha.getBytes()));
    sen = hash.toString(16);
    return sen;
}

From source file:Main.java

private static String computeSignature(String baseString, String keyString)
        throws GeneralSecurityException, UnsupportedEncodingException {
    Mac mac = Mac.getInstance("HmacSHA256");
    SecretKeySpec secret = new SecretKeySpec(keyString.getBytes("UTF-8"), "HmacSHA256");
    mac.init(secret);/*from   www.  java 2s.c o  m*/
    byte[] byteData = mac.doFinal(baseString.getBytes("UTF-8"));

    BigInteger hash = new BigInteger(1, byteData);
    String hmac = hash.toString(16);

    if (hmac.length() % 2 != 0) {
        hmac = "0" + hmac;
    }

    return hmac;
}