Java Hex Calculate toHexString(byte[] block)

Here you can find the source of toHexString(byte[] block)

Description

Converts a byte array to hex string

License

Open Source License

Declaration

public static String toHexString(byte[] block) 

Method Source Code

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

public class Main {
    /**//from w  w w  .  j av  a 2 s . c o  m
     * Converts a byte array to hex string
     */
    public static String toHexString(byte[] block) {
        StringBuffer buf = new StringBuffer();
        int len = block.length;
        for (int i = 0; i < len; i++) {
            byte2hex(block[i], buf);
            if (i < len - 1) {
                buf.append(":");
            }
        }
        return buf.toString();
    }

    /**
     * Converts a byte to hex digit and writes to the supplied buffer
     */
    private static void byte2hex(byte b, StringBuffer buf) {
        char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        int high = ((b & 0xf0) >> 4);
        int low = (b & 0x0f);
        buf.append(hexChars[high]);
        buf.append(hexChars[low]);
    }
}

Related

  1. toHexString(byte[] ba)
  2. toHexString(byte[] ba)
  3. toHexString(byte[] ba, int offset, int length)
  4. toHexString(byte[] binary)
  5. toHexString(byte[] binaryData)
  6. toHexString(byte[] buf)
  7. toHexString(byte[] buf)
  8. toHexString(byte[] buf, int offset, int len)
  9. toHexString(byte[] buffer)