Java Hex Calculate toHex(byte[] input)

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

Description

to Hex

License

Apache License

Declaration

static String toHex(byte[] input) 

Method Source Code

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

public class Main {
    static String toHex(byte[] input) {
        StringBuilder sb = new StringBuilder();
        for (byte b : input) {
            int i = (b >> 4) & 0x0f;
            if (i > 9) {
                sb.append((char) (i + 55));
            } else {
                sb.append((char) (i + 48));
            }//  www .j  a  va2  s.  c o  m
            i = b & 0x0f;
            if (i > 9) {
                sb.append((char) (i + 55));
            } else {
                sb.append((char) (i + 48));
            }
        }
        return sb.toString();
    }

    /**
     * if (len(input)<offset+length) throw toHex(input[offset,input.length])
     *
     * @param input
     * @param offset
     * @param length max length
     * @return
     */
    static String toHex(byte[] input, int offset, int length) {
        byte[] bytes = getSomeByte(input, offset, length);
        return toHex(bytes);
    }

    /**
     * @param input
     * @param offset
     * @param length
     * @return
     */
    static byte[] getSomeByte(byte[] input, int offset, int length) {
        byte[] bytes = new byte[length];
        System.arraycopy(input, offset, bytes, 0, length);
        return bytes;
    }
}

Related

  1. toHex(byte[] data, int perLine, boolean offset)
  2. toHex(byte[] dBytes)
  3. toHex(byte[] digest)
  4. toHex(byte[] digest)
  5. toHex(byte[] inBytes)
  6. toHex(byte[] key)
  7. toHex(byte[] raw)
  8. toHex(byte[] raw)
  9. toHex(byte[] raw)