Java Byte Array to Hex String bytesToHexString(byte abyte0[], int i, int j)

Here you can find the source of bytesToHexString(byte abyte0[], int i, int j)

Description

bytes To Hex String

License

Open Source License

Declaration

public static String bytesToHexString(byte abyte0[], int i, int j) 

Method Source Code

//package com.java2s;
/**/*from  ww  w  .j av a  2  s.c o m*/
 * This file is part of JEMMA - http://jemma.energy-home.org
 * (C) Copyright 2013 Telecom Italia (http://www.telecomitalia.it)
 *
 * JEMMA is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License (LGPL) version 3
 * or later as published by the Free Software Foundation, which accompanies
 * this distribution and is available at http://www.gnu.org/licenses/lgpl.html
 *
 * JEMMA 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 (LGPL) for more details.
 *
 */

public class Main {
    static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6',
            '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    public static String bytesToHexString(byte abyte0[], int i, int j) {
        StringBuffer stringbuffer = new StringBuffer("[");
        int k = Math.min(i + j, abyte0.length);
        for (int l = i; l < k; l++) {
            stringbuffer.append("0x");
            stringbuffer.append(toHexString(0xff & abyte0[l]));
            if (l < k - 1)
                stringbuffer.append(", ");
        }

        stringbuffer.append("]");
        return stringbuffer.toString();
    }

    public static String bytesToHexString(byte abyte0[]) {
        if (abyte0 == null)
            return "null";
        else
            return bytesToHexString(abyte0, 0, abyte0.length);
    }

    public static String toHexString(int i) {
        char ac[] = new char[32];
        int j = 32;
        byte byte0 = 16;
        int k = byte0 - 1;
        do {
            ac[--j] = HEX_DIGITS[i & k];
            i >>>= 4;
        } while (i != 0);
        return new String(ac, j, 32 - j);
    }
}

Related

  1. bytesToHexSpaced(byte[] bytes)
  2. bytesToHexStr(byte[] bcd)
  3. bytesToHexStr(byte[] bytes)
  4. bytesToHexStr(byte[] bytes, int offset, int size)
  5. bytesToHexString(byte abyte0[])
  6. bytesToHexString(byte bytes[])
  7. bytesToHexString(byte data[])
  8. bytesToHexString(byte[] _bytes)
  9. bytesToHexString(byte[] array)