Java Byte Array to Hex String bytesToHexString(byte[] in, int length)

Here you can find the source of bytesToHexString(byte[] in, int length)

Description

bytes To Hex String

License

Open Source License

Declaration

public static char[] bytesToHexString(byte[] in, int length) 

Method Source Code

//package com.java2s;
/**//w w w  .  j ava 2 s .c o m
 * Copyright (C) 2006-2008 Werner Dittmann
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Werner Dittmann <Werner.Dittmann@t-online.de>
 */

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

    public static char[] bytesToHexString(byte[] in, int length) {
        if (length > in.length)
            return null;

        char[] out = new char[length * 2];

        for (int i = 0; i < length; i++) {
            byte b = in[i];
            out[i * 2] = hex[(b >>> 4) & 0xf];
            out[i * 2 + 1] = hex[b & 0xf];
        }
        return out;
    }
}

Related

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