Java BigInteger Calculate dump(BigInteger x)

Here you can find the source of dump(BigInteger x)

Description

Returns a hexadecimal dump of the trimmed bytes of a BigInteger .

License

Open Source License

Parameter

Parameter Description
x the BigInteger to display.

Return

the string representation of the designated .

Declaration

public static final String dump(BigInteger x) 

Method Source Code


//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.math.BigInteger;

public class Main {
    private static final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();

    /**// www  .  j a  v a 2 s. c o m
     * <p>Returns a hexadecimal dump of the trimmed bytes of a {@link BigInteger}.
     * </p>
     *
     * @param x the {@link BigInteger} to display.
     * @return the string representation of the designated {@link BigInteger}.
     */
    public static final String dump(BigInteger x) {
        return dumpString(trim(x));
    }

    /**
     * <p>Dumps a byte array as a string, in a format that is easy to read for
     * debugging. The string <code>m</code> is prepended to the start of each
     * line.</p>
     *
     * <p>If <code>offset</code> and <code>length</code> are omitted, the whole
     * array is used. If <code>m</code> is omitted, nothing is prepended to each
     * line.</p>
     *
     * @param data the byte array to be dumped.
     * @param offset the offset within <i>data</i> to start from.
     * @param length the number of bytes to dump.
     * @param m a string to be prepended to each line.
     * @return a string containing the result.
     */
    public static String dumpString(byte[] data, int offset, int length, String m) {
        if (data == null) {
            return m + "null\n";
        }
        StringBuffer sb = new StringBuffer(length * 3);
        if (length > 32) {
            sb.append(m).append("Hexadecimal dump of ").append(length).append(" bytes...\n");
        }
        // each line will list 32 bytes in 4 groups of 8 each
        int end = offset + length;
        String s;
        int l = Integer.toString(length).length();
        if (l < 4) {
            l = 4;
        }
        for (; offset < end; offset += 32) {
            if (length > 32) {
                s = "         " + offset;
                sb.append(m).append(s.substring(s.length() - l)).append(": ");
            }
            int i = 0;
            for (; i < 32 && offset + i + 7 < end; i += 8) {
                sb.append(toString(data, offset + i, 8)).append(' ');
            }
            if (i < 32) {
                for (; i < 32 && offset + i < end; i++) {
                    sb.append(byteToString(data[offset + i]));
                }
            }
            sb.append('\n');
        }
        return sb.toString();
    }

    public static String dumpString(byte[] data) {
        return (data == null) ? "null\n" : dumpString(data, 0, data.length, "");
    }

    public static String dumpString(byte[] data, String m) {
        return (data == null) ? "null\n" : dumpString(data, 0, data.length, m);
    }

    public static String dumpString(byte[] data, int offset, int length) {
        return dumpString(data, offset, length, "");
    }

    /**
     * <p>Treats the input as the MSB representation of a number, and discards
     * leading zero elements. For efficiency, the input is simply returned if no
     * leading zeroes are found.</p>
     *
     * @param n the {@link BigInteger} to trim.
     * @return the byte array representation of the designated {@link BigInteger}
     * with no leading 0-bytes.
     */
    public static final byte[] trim(BigInteger n) {
        byte[] in = n.toByteArray();
        if (in.length == 0 || in[0] != 0) {
            return in;
        }
        int len = in.length;
        int i = 1;
        while (in[i] == 0 && i < len) {
            ++i;
        }
        byte[] result = new byte[len - i];
        System.arraycopy(in, i, result, 0, len - i);
        return result;
    }

    /**
     * <p>Returns a string of hexadecimal digits from a byte array. Each byte is
     * converted to 2 hex symbols; zero(es) included.</p>
     *
     * <p>This method calls the method with same name and three arguments as:</p>
     *
     * <pre>
     *    toString(ba, 0, ba.length);
     * </pre>
     *
     * @param ba the byte array to convert.
     * @return a string of hexadecimal characters (two for each byte)
     * representing the designated input byte array.
     */
    public static String toString(byte[] ba) {
        return toString(ba, 0, ba.length);
    }

    /**
     * <p>Returns a string of hexadecimal digits from a byte array, starting at
     * <code>offset</code> and consisting of <code>length</code> bytes. Each byte
     * is converted to 2 hex symbols; zero(es) included.</p>
     *
     * @param ba the byte array to convert.
     * @param offset the index from which to start considering the bytes to
     * convert.
     * @param length the count of bytes, starting from the designated offset to
     * convert.
     * @return a string of hexadecimal characters (two for each byte)
     * representing the designated input byte sub-array.
     */
    public static final String toString(byte[] ba, int offset, int length) {
        char[] buf = new char[length * 2];
        for (int i = 0, j = 0, k; i < length;) {
            k = ba[offset + i++];
            buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
            buf[j++] = HEX_DIGITS[k & 0x0F];
        }
        return new String(buf);
    }

    /**
     * <p>Returns a string of 8 hexadecimal digits (most significant digit first)
     * corresponding to the unsigned integer <code>n</code>.</p>
     *
     * @param n the unsigned integer to convert.
     * @return a hexadecimal string 8-character long.
     */
    public static String toString(int n) {
        char[] buf = new char[8];
        for (int i = 7; i >= 0; i--) {
            buf[i] = HEX_DIGITS[n & 0x0F];
            n >>>= 4;
        }
        return new String(buf);
    }

    /**
     * <p>Returns a string of hexadecimal digits from an integer array. Each int
     * is converted to 4 hex symbols.</p>
     */
    public static String toString(int[] ia) {
        int length = ia.length;
        char[] buf = new char[length * 8];
        for (int i = 0, j = 0, k; i < length; i++) {
            k = ia[i];
            buf[j++] = HEX_DIGITS[(k >>> 28) & 0x0F];
            buf[j++] = HEX_DIGITS[(k >>> 24) & 0x0F];
            buf[j++] = HEX_DIGITS[(k >>> 20) & 0x0F];
            buf[j++] = HEX_DIGITS[(k >>> 16) & 0x0F];
            buf[j++] = HEX_DIGITS[(k >>> 12) & 0x0F];
            buf[j++] = HEX_DIGITS[(k >>> 8) & 0x0F];
            buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
            buf[j++] = HEX_DIGITS[k & 0x0F];
        }
        return new String(buf);
    }

    /**
     * <p>Returns a string of 16 hexadecimal digits (most significant digit first)
     * corresponding to the unsigned long <code>n</code>.</p>
     *
     * @param n the unsigned long to convert.
     * @return a hexadecimal string 16-character long.
     */
    public static String toString(long n) {
        char[] b = new char[16];
        for (int i = 15; i >= 0; i--) {
            b[i] = HEX_DIGITS[(int) (n & 0x0FL)];
            n >>>= 4;
        }
        return new String(b);
    }

    /**
     * <p>Returns a string of 2 hexadecimal digits (most significant digit first)
     * corresponding to the lowest 8 bits of <code>n</code>.</p>
     *
     * @param n the byte value to convert.
     * @return a string of 2 hex characters representing the input.
     */
    public static String byteToString(int n) {
        char[] buf = { HEX_DIGITS[(n >>> 4) & 0x0F], HEX_DIGITS[n & 0x0F] };
        return new String(buf);
    }
}

Related

  1. DiffieHellman(BigInteger p, BigInteger g, BigInteger x, BigInteger y, BigInteger y_B)
  2. distance(BigInteger a, BigInteger b)
  3. distance(BigInteger a, BigInteger b)
  4. div(BigInteger dividend, BigInteger divisor)
  5. divide(BigInteger dividend, BigInteger divisor)
  6. dumpBitLengthOfValues(BigInteger... data)
  7. dumpDataPathId(BigInteger datapathId)
  8. dumpNumber(PrintStream out, String s, BigInteger bi)
  9. emit(BigInteger integer)