Java Byte Array to Hex byteToHex(byte[] b, int size)

Here you can find the source of byteToHex(byte[] b, int size)

Description

byte To Hex

License

Open Source License

Declaration

public static String byteToHex(byte[] b, int size) 

Method Source Code

//package com.java2s;

public class Main {

    public static String byteToHex(byte[] b, int size) {
        return byteToHex(b, size, true);
    }/*  www .  ja  v  a2s.c o m*/

    public static String byteToHex(byte[] b, int size, boolean left) {
        byte[] d = new byte[size];

        if (left) { //left size
            System.arraycopy(b, 0, d, 0, (b.length > size) ? size : b.length);
        } else { //right size
            System.arraycopy(b, (b.length > size) ? (b.length - size) : 0, d, 0,
                    (b.length > size) ? size : b.length);
        }

        return byteToHex(d);
    }

    public static String byteToHex(byte[] b) {
        return byteToHex(b, true);
    }

    public static String byteToHex(byte[] b, boolean format) {
        StringBuilder toHex = new StringBuilder();

        for (int i = 0; (null != b) && (i < b.length); i++) {
            if (format) {
                toHex.append(' ');
            }

            char hi = Character.forDigit((b[i] >> 4) & 0x0F, 16);
            char lo = Character.forDigit(b[i] & 0x0F, 16);
            toHex.append(Character.toUpperCase(hi));
            toHex.append(Character.toUpperCase(lo));

            if (format && 15 == i % 16) {
                toHex.append('\r').append('\n');
            }
        }

        return toHex.toString();
    }
}

Related

  1. byteToHex(byte b[])
  2. byteToHex(byte bytes[])
  3. byteToHex(byte data)
  4. byteToHex(byte[] array)
  5. byteToHex(byte[] array, String separator)
  6. byteToHex(byte[] base)
  7. byteToHex(byte[] buf)
  8. byteToHex(byte[] buffer)
  9. ByteToHex(byte[] bytes)