Java Byte Array to Hex String bytesToHexString(byte[] mpi)

Here you can find the source of bytesToHexString(byte[] mpi)

Description

bytes To Hex String

License

Open Source License

Declaration

static String bytesToHexString(byte[] mpi) 

Method Source Code

//package com.java2s;
/*/* w  w  w.  j a v  a2s  . com*/
 *  Java OTR library
 *  Copyright (C) 2008-2009  Ian Goldberg, Muhaimeen Ashraf, Andrew Chung,
 *                           Can Tang
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of version 2.1 of the GNU Lesser General
 *  Public License as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

public class Main {
    static String bytesToHexString(byte[] mpi) {
        byte[] hex = new byte[2 * mpi.length];
        for (int i = 0; i < mpi.length; i++) {
            int num = (int) (mpi[i] >> 4 & 0xf);
            if (num <= 9) {
                hex[2 * i] = (byte) ('0' + num);
            } else {
                hex[2 * i] = (byte) ('A' + num - 10);
            }
            num = (int) (mpi[i] & 0xf);
            if (num <= 9) {
                hex[2 * i + 1] = (byte) ('0' + num);
            } else {
                hex[2 * i + 1] = (byte) ('A' + num - 10);
            }

        }
        return new String(hex);
    }
}

Related

  1. bytesToHexString(byte[] data, int fromIndex, int toIndex)
  2. bytesToHexString(byte[] data, int offset, int length)
  3. bytesToHexString(byte[] hasher)
  4. bytesToHexString(byte[] in, int length)
  5. bytesToHexString(byte[] input)
  6. bytesToHexString(byte[] src)
  7. bytesToHexString(byte[] src)
  8. bytesToHexString(final byte[] bytes)
  9. bytesToHexString(final byte[] bytes)