Java BigInteger Format formatBigInteger(BigInteger bi, int length)

Here you can find the source of formatBigInteger(BigInteger bi, int length)

Description

format Big Integer

License

Apache License

Declaration

public static String formatBigInteger(BigInteger bi, int length) 

Method Source Code

//package com.java2s;
/*//  w w  w  . ja  v  a 2  s . com
 * $HeadURL: http://juliusdavies.ca/svn/not-yet-commons-ssl/tags/commons-ssl-0.3.16/src/java/org/apache/commons/ssl/PEMUtil.java $
 * $Revision: 153 $
 * $Date: 2009-09-15 22:40:53 -0700 (Tue, 15 Sep 2009) $
 *
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */

import java.math.BigInteger;

public class Main {
    static final String LINE_SEPARATOR = System.getProperty("line.separator");

    public static String formatBigInteger(BigInteger bi, int length) {
        String s = bi.toString(16);
        StringBuffer buf = new StringBuffer(s.length());
        int zeroesToAppend = length - s.length();
        int count = 0;
        buf.append("    ");
        for (int i = 0; i < zeroesToAppend; i++) {
            count++;
            buf.append('0');
            if (i % 2 == 1) {
                buf.append(':');
            }
        }
        for (int i = 0; i < s.length() - 2; i++) {
            count++;
            buf.append(s.charAt(i));
            if (i % 2 == 1) {
                buf.append(':');
            }
            if (count % 30 == 0) {
                buf.append(LINE_SEPARATOR);
                buf.append("    ");
            }
        }
        buf.append(s.substring(s.length() - 2));
        return buf.toString();
    }
}

Related

  1. formatBigIntegerBinary(final long value, byte[] buf, final int offset, final int length, final boolean negative)
  2. formatSerialNumber(BigInteger bi)
  3. formatSerialNumber(final BigInteger serial)
  4. formatSize(BigInteger size)