Android Byte Array to Hex Convert bytesToHex(byte[] bytes, boolean withSpaces)

Here you can find the source of bytesToHex(byte[] bytes, boolean withSpaces)

Description

Takes the provided byte array and converts it into a hexidecimal string with two characters per byte.

License

Open Source License

Parameter

Parameter Description
withSpaces if true, this will put a space character between each hex-rendered byte, for readability.

Declaration

public static String bytesToHex(byte[] bytes, boolean withSpaces) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from   ww w. j  a v a 2s.  co  m
     * Takes the provided byte array and converts it into a hexidecimal string
     * with two characters per byte.
     */
    public static String bytesToHex(byte[] bytes) {
        return bytesToHex(bytes, false);
    }

    /**
     * Takes the provided byte array and converts it into a hexidecimal string
     * with two characters per byte.
     * 
     * @param withSpaces
     *          if true, this will put a space character between each hex-rendered
     *          byte, for readability.
     */
    public static String bytesToHex(byte[] bytes, boolean withSpaces) {
        StringBuilder sb = new StringBuilder();
        for (byte hashByte : bytes) {
            int intVal = 0xff & hashByte;
            if (intVal < 0x10) {
                sb.append('0');
            }
            sb.append(Integer.toHexString(intVal));
            if (withSpaces) {
                sb.append(' ');
            }
        }
        return sb.toString();
    }
}

Related

  1. decodeHex(char[] data)
  2. bytesToHex(byte[] data)
  3. bytesToHexString(byte[] bytesArray)
  4. parseByte2HexStr(byte buf[])
  5. bytesToHex(byte[] bytes)
  6. byteArrayToHexString(byte[] bytes)
  7. byteArrayToHexString(byte[] bytes)
  8. bytesToHex(byte[] data)
  9. convertToHex(byte[] in)