Java Hex Calculate toHexString(byte[] bytes)

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

Description

Convert bytes to hex string.

License

Apache License

Declaration

public static String toHexString(byte[] bytes) 

Method Source Code

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

public class Main {
    private static String[] int2Hex = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B",
            "C", "D", "E", "F", };

    /**/*w  ww.jav a  2  s .  c o  m*/
     * Convert bytes to hex string.
     * */
    public static String toHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(toHexString(b) + " ");
        }
        return sb.toString();
    }

    /**
     * Convert byte to hex string.
     * */
    private static String toHexString(byte b) {
        StringBuilder sb = new StringBuilder();

        int r1 = (b >>> 4) & 0xF;
        sb.append(int2Hex[r1]);
        int r2 = b & 0xF;
        sb.append(int2Hex[r2]);
        return sb.toString();
    }
}

Related

  1. toHexString(byte[] bytes)
  2. toHexString(byte[] bytes)
  3. toHexString(byte[] bytes)
  4. toHexString(byte[] bytes)
  5. toHexString(byte[] bytes)
  6. toHexString(byte[] bytes)
  7. toHexString(byte[] bytes)
  8. toHexString(byte[] bytes)
  9. toHexString(byte[] bytes)