Java BigInteger Format formatSerialNumber(final BigInteger serial)

Here you can find the source of formatSerialNumber(final BigInteger serial)

Description

Get a proper string representation of a certificate's serial number.

License

Open Source License

Parameter

Parameter Description
serial The certificate's serial number.

Return

A string representation of the serial number.

Declaration

public static String formatSerialNumber(final BigInteger serial) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 * Copyright (c) 2008 g-Eclipse Consortium 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Initial development of the original code was made for the
 * g-Eclipse project founded by European Union
 * project number: FP6-IST-034327  http://www.geclipse.eu/
 *
 * Contributors:/*  w  w  w  .  j  av  a2s  .c o  m*/
 *    Mathias Stuempert - initial API and implementation
 *****************************************************************************/

import java.math.BigInteger;

public class Main {
    /**
     * Get a proper string representation of a certificate's serial number.
     * 
     * @param serial The certificate's serial number.
     * @return A string representation of the serial number.
     */
    public static String formatSerialNumber(final BigInteger serial) {

        StringBuffer b = new StringBuffer(serial.toString(16).toUpperCase());

        if ((b.length() % 2) != 0) {
            b.insert(0, "0"); //$NON-NLS-1$
        }

        int index = 2;
        for (index = 2; index < b.length(); index += 3) {
            b.insert(index, ":"); //$NON-NLS-1$
        }

        return b.toString();

    }
}

Related

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