Java Hex Calculate toHexFromBytes(byte[] bytes)

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

Description

Converts array of bytes

License

Apache License

Parameter

Parameter Description
bytes byte array to be converted to hex string

Return

hex string

Declaration

public static final StringBuilder toHexFromBytes(byte[] bytes) 

Method Source Code

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

public class Main {
    private static final String[] hexSymbols = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c",
            "d", "e", "f" };

    /***//w w w . j  ava2 s.com
     * Converts array of bytes
     * 
     * @param bytes
     *            byte array to be converted to hex string
     * @return hex string
     */
    public static final StringBuilder toHexFromBytes(byte[] bytes) {

        StringBuilder hexBuilder = null;

        if ((bytes != null) && (bytes.length > 0)) {
            hexBuilder = new StringBuilder(bytes.length * 2);
            for (int i = 0; i < bytes.length; i++) {
                hexBuilder.append(toHexFromByte(bytes[i]));
            }
        } else {
            hexBuilder = new StringBuilder("");
        }

        return hexBuilder;
    }

    /***
     * Converts byte to hex string.
     * 
     * @param b
     *            byte to be converted to hex string
     * @return hex string
     */
    public static final StringBuilder toHexFromByte(byte b) {
        byte leftSymbol = (byte) (b >>> 4 & 0xF);
        byte rightSymbol = (byte) (b & 0xF);
        return new StringBuilder(hexSymbols[leftSymbol] + hexSymbols[rightSymbol]);
    }
}

Related

  1. toHexEscape(final int u0)
  2. toHexFilter(String inAscii)
  3. toHexFromBin(final String binSymbols)
  4. toHexFromByte(byte b)
  5. toHexFromByte(final byte b)
  6. toHexFromBytes(final byte[] bytes)
  7. toHexFromOct(final String octSymbols)
  8. toHexHashString(byte[] id)
  9. toHexLiteral(int v)