Java Hex Calculate toHex(final byte[] data)

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

Description

Converts data to an hexadecimal string

License

LGPL

Parameter

Parameter Description
data - Data to convert to hexadecimal

Return

String in hexadecimal

Declaration

private static String toHex(final byte[] data) 

Method Source Code

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

public class Main {
    /**/*from  w w  w  .jav a 2  s.  c om*/
     * Converts data to an hexadecimal string
     *
     * @param data
     *            - Data to convert to hexadecimal
     * @return String in hexadecimal
     */
    private static String toHex(final byte[] data) {
        final StringBuffer buf = new StringBuffer();
        for (final byte element : data) {
            int halfbyte = (element >>> 4) & 0x0F;
            int two_halfs = 0;
            do {
                if ((0 <= halfbyte) && (halfbyte <= 9)) {
                    buf.append((char) ('0' + halfbyte));
                } else {
                    buf.append((char) ('a' + (halfbyte - 10)));
                }
                halfbyte = element & 0x0F;
            } while (two_halfs++ < 1);
        }
        return buf.toString();
    }
}

Related

  1. toHex(final byte[] bytes)
  2. toHex(final byte[] bytes)
  3. toHex(final byte[] bytes)
  4. tohex(final byte[] bytes)
  5. toHex(final byte[] bytes, final int offset, final int count)
  6. toHex(final byte[] data)
  7. toHex(final byte[] data)
  8. toHex(final byte[] data, final int startPos, final int length)
  9. toHex(final byte[] data, final String separator)