Java Hex Calculate toHex(byte bytes[])

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

Description

to Hex

License

Open Source License

Declaration

public static String toHex(byte bytes[]) 

Method Source Code

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

public class Main {
    private static final String CHARS = "0123456789ABCDEF";

    public static String toHex(int d) {
        int r = d % 16;
        String c = toChar(r);// ww  w . ja v a2 s  .com
        int diff = d - r;
        if (diff == 0) {
            return c;
        } else {
            int next = diff / 16;
            return toHex(next) + c;
        }
    }

    public static String toHex(byte bytes[]) {
        StringBuffer buf = new StringBuffer(bytes.length * 2);
        int i;

        for (i = 0; i < bytes.length; i++) {
            if ((bytes[i] & 0xff) < 0x10) {
                buf.append("0");
            }
            buf.append(Long.toString(bytes[i] & 0xff, 16));
        }
        return buf.toString();
    }

    private static String toChar(int c) {
        int pos = Math.abs(c);
        return String.valueOf(CHARS.charAt(pos));
    }
}

Related

  1. toHex(byte b)
  2. toHex(byte b)
  3. toHex(byte b)
  4. toHex(byte b, char[] charArray, int from)
  5. toHex(byte b, String prefix)
  6. toHex(byte data[])
  7. toHex(byte hash[])
  8. toHex(byte in)
  9. toHex(byte in[])