Android Byte Array to Hex Convert toHexString(byte[] bytes)

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

Description

to Hex String

Declaration

public static String toHexString(byte[] bytes) 

Method Source Code

//package com.java2s;

public class Main {

    public static String toHexString(byte b) {
        StringBuffer result = new StringBuffer(3);
        result.append(Integer.toString((b & 0xF0) >> 4, 16));
        result.append(Integer.toString(b & 0x0F, 16));
        return result.toString();
    }//w  w  w  . j ava 2 s . c o m

    public static String toHexString(byte[] bytes) {
        if (bytes == null) {
            return null;
        }

        StringBuffer result = new StringBuffer();
        for (byte b : bytes) {
            result.append(Integer.toString((b & 0xF0) >> 4, 16));
            result.append(Integer.toString(b & 0x0F, 16));
        }
        return result.toString();
    }

    public static String toHexString(byte[] bytes, int offset, int length) {
        if (bytes == null) {
            return null;
        }

        StringBuffer result = new StringBuffer();
        for (int i = offset; i < offset + length; i++) {
            result.append(Integer.toString((bytes[i] & 0xF0) >> 4, 16));
            result.append(Integer.toString(bytes[i] & 0x0F, 16));
        }
        return result.toString();
    }
}

Related

  1. bytesToHexString(byte[] src)
  2. bytesToHexString(byte[] src)
  3. bytesToHexString(byte[] values)
  4. bytesAsHexString(byte[] bytes, int maxShowBytes)
  5. toHexString(byte[] bytes)
  6. toHexString(byte[] bytes, int numBytes)
  7. toHexString(byte[] bytes, int offset, int length)
  8. toHexString(byte[] data)
  9. toHexString(byte[] data, int offset, int length)