Java Byte Array to Hex bytes2HexSplit(byte[] in, int wordlength)

Here you can find the source of bytes2HexSplit(byte[] in, int wordlength)

Description

From Van Bui - prints out a hex string formatted with spaces between each hex word of length wordlength.

License

Open Source License

Parameter

Parameter Description
in input array of bytes to convert
wordlength the length of hex words to print otu.

Declaration

public static String bytes2HexSplit(byte[] in, int wordlength) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from w  ww. j  a v a  2 s  .co  m
     *    From Van Bui - prints out a hex string formatted with
     *    spaces between each hex word of length wordlength.
     *
     *    @param in input array of bytes to convert
     *    @param wordlength the length of hex words to print otu.
     */
    public static String bytes2HexSplit(byte[] in, int wordlength) {
        String hex = bytes2Hex(in);
        StringBuffer buff = new StringBuffer();

        for (int i = 0; i < hex.length(); i++) {
            buff.append(hex.charAt(i));
            if ((i + 1) % wordlength == 0)
                buff.append(" ");
        }

        return buff.toString();
    }

    /**
     *    From Van Bui - prints out a hex string formatted with
     *    spaces between each hex word of length wordlength, and
     *    new lines every linelength.
     *
     *    @param in input array of bytes to convert
     *    @param wordlength the length of hex words to print otu.
     *    @param linelength the length of a line to print before inserting
     *           a line feed.
     */

    public static String bytes2HexSplit(byte[] in, int wordlength, int linelength) {
        String hex = bytes2Hex(in);
        StringBuffer buff = new StringBuffer();

        for (int i = 0; i < hex.length(); i++) {
            buff.append(hex.charAt(i));
            if ((i + 1) % wordlength == 0)
                buff.append(" ");
            if ((i + 1) % linelength == 0)
                buff.append("\n");
        }

        return buff.toString();
    }

    public static String bytes2Hex(byte[] bytes) {
        StringBuffer ret = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            ret.append(byte2Hex(bytes[i]));
        }
        return ret.toString();
    }

    static public String byte2Hex(byte b) {
        // Returns hex String representation of byte b
        final char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        char[] array = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] };
        return new String(array);
    }
}

Related

  1. bytes2Hex(byte[] bytes)
  2. bytes2Hex(byte[] bytes)
  3. bytes2Hex(byte[] bytes)
  4. bytes2HexCharArr(byte[] bytes)
  5. bytes2HexPP2(byte[] bytes)
  6. bytes2HexStr(byte[] bytes)
  7. bytes2HexStr(byte[] bytes)
  8. bytes2HexString(byte... bytes)
  9. bytes2HexString(byte[] array)