Java Byte Array to Hex bytesToHex(byte[] bytes, int size, char delim)

Here you can find the source of bytesToHex(byte[] bytes, int size, char delim)

Description

Convert bytes to hex string

License

Open Source License

Parameter

Parameter Description
bytes a parameter

Return

hex string

Declaration


public static String bytesToHex(byte[] bytes, int size, char delim) 

Method Source Code

//package com.java2s;
/**//from   w ww  . j a  v a 2s .co m
 * Copyright (c) 2014 xio4
 * Universal bot for lineage-like games (Archeage, Lineage2 etc)
 *
 * This file is part of Unibot.
 *
 * Unibot 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/>.
 */

public class Main {
    final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();

    /**
     * Convert bytes to hex string
     * 
     * @param bytes
     * @return hex string
     */

    public static String bytesToHex(byte[] bytes, int size, char delim) {
        char[] hexChars;
        if (bytes == null)
            return "";
        if (delim == 0) {
            hexChars = new char[size * 2];
            for (int j = 0; j < size; j++) {
                int v = bytes[j] & 0xFF;
                hexChars[j * 2] = hexArray[v >>> 4];
                hexChars[j * 2 + 1] = hexArray[v & 0x0F];
            }
        } else {
            hexChars = new char[size * 3];
            for (int j = 0; j < size; j++) {
                int v = bytes[j] & 0xFF;
                hexChars[j * 3] = hexArray[v >>> 4];
                hexChars[j * 3 + 1] = hexArray[v & 0x0F];
                hexChars[j * 3 + 2] = delim;
            }
        }
        return new String(hexChars);
    }

    public static String bytesToHex(byte[] bytes, char delim) {
        return bytesToHex(bytes, bytes.length, delim);

    }
}

Related

  1. bytesToHex(byte[] bytes)
  2. bytesToHex(byte[] bytes)
  3. bytesToHex(byte[] bytes)
  4. bytesToHex(byte[] bytes, byte[] hex, int offset)
  5. bytesToHex(byte[] bytes, int groupSize)
  6. bytesToHex(byte[] data)
  7. bytesToHex(byte[] data)
  8. bytesToHex(byte[] data)
  9. bytesToHex(byte[] data)