Java Hex Calculate toHexString(byte[] val)

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

Description

to Hex String

License

Open Source License

Declaration

public static String toHexString(byte[] val) 

Method Source Code

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

public class Main {
    public static String toHexString(byte[] val) {
        return toHexString(val, 0, val.length);
    }/*  ww  w .j  av a  2s.c o  m*/

    public static String toHexString(byte[] val, int offset, int length) {
        long lVal = 0;
        int cnt = length / 8;
        int startIndex = offset;
        StringBuilder hexStr = new StringBuilder();

        for (int i = 0; i < cnt; i++) {

            lVal = ((((long) val[startIndex]) << 56) & 0xFF00000000000000L)
                    + ((((long) val[startIndex + 1]) << 48) & 0x00FF000000000000L)
                    + ((((long) val[startIndex + 2]) << 40) & 0x0000FF0000000000L)
                    + ((((long) val[startIndex + 3]) << 32) & 0x000000FF00000000L)
                    + ((((long) val[startIndex + 4]) << 24) & 0x00000000FF000000L)
                    + ((((long) val[startIndex + 5]) << 16) & 0x0000000000FF0000L)
                    + ((((long) val[startIndex + 6]) << 8) & 0x000000000000FF00L)
                    + ((((long) val[startIndex + 7])) & 0x00000000000000FFL);
            hexStr.append(toHexString(lVal));

            startIndex += 8;
        }

        for (; startIndex < length; startIndex++) {
            hexStr.append(toHexString(val[startIndex]));
        }

        return hexStr.toString();
    }

    public static String toHexString(long val) {
        StringBuilder hexStr = new StringBuilder(Long.toHexString(val));

        for (int i = hexStr.length(); i < 16; i++) {
            hexStr.insert(0, '0');
        }

        return hexStr.toString();
    }
}

Related

  1. toHexString(byte[] raw)
  2. toHexString(byte[] raw)
  3. toHexString(byte[] src)
  4. toHexString(byte[] v)
  5. toHexString(byte[] v)
  6. toHexString(byte[] value)
  7. toHexString(byte[] value)
  8. toHexString(byte[] value, int startOffset, int maxLength, boolean uppercase, char separator)
  9. toHexString(char c)