Java Hex Calculate toHex(final byte[] data, final String separator)

Here you can find the source of toHex(final byte[] data, final String separator)

Description

to Hex

License

Apache License

Parameter

Parameter Description
data Data to encode.
separator How to separate the bytes in the output

Return

The byte array encoded as a string of hex digits separated by given separator.

Declaration

public static String toHex(final byte[] data, final String separator) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

    public static char toHex(final int nibble) {
        return HEX_DIGIT[(nibble & MAX_HEX_DIGIT)];
    }/* w  w w.jav  a  2 s  .  c  om*/

    /**
     * @param data      Data to encode.
     * @param separator How to separate the bytes in the output
     * @return The byte array encoded as a string of hex digits separated by given separator.
     */
    public static String toHex(final byte[] data, final String separator) {
        if (data.length == 0) {
            return "";
        }

        final StringBuilder sb = new StringBuilder(data.length * (separator.length() + 2) - separator.length());

        for (int i = 0; i < data.length; i++) {
            if (i > 0) {
                sb.append(separator);
            }
            sb.append(toHex((data[i] & 0xF0) >> 4));
            sb.append(toHex(data[i] & 0x0F));
        }

        return sb.toString();
    }
}

Related

  1. toHex(final byte[] bytes, final int offset, final int count)
  2. toHex(final byte[] data)
  3. toHex(final byte[] data)
  4. toHex(final byte[] data)
  5. toHex(final byte[] data, final int startPos, final int length)
  6. toHex(final byte[] input)
  7. toHex(final byte[] value)
  8. toHex(final char a, final int halfbyte)
  9. toHex(final char[] dest, int destPos, final byte[] src, final int srcStart, final int srcLength)