Android Byte Array to Hex Convert bytesAsHexString(byte[] bytes, int maxShowBytes)

Here you can find the source of bytesAsHexString(byte[] bytes, int maxShowBytes)

Description

bytes As Hex String

Declaration

public static String bytesAsHexString(byte[] bytes, int maxShowBytes) 

Method Source Code

//package com.java2s;

public class Main {
    public static String bytesAsHexString(byte[] bytes, int maxShowBytes) {
        int idx = 0;
        StringBuilder body = new StringBuilder();
        body.append("bytes size is:[");
        body.append(bytes.length);/*from w w w  .ja  v  a2s. c o m*/
        body.append("]\r\n");

        for (byte b : bytes) {
            int hex = ((int) b) & 0xff;
            String shex = Integer.toHexString(hex).toUpperCase();
            if (1 == shex.length()) {
                body.append("0");
            }
            body.append(shex);
            body.append(" ");
            idx++;
            if (16 == idx) {
                body.append("\r\n");
                idx = 0;
            }
            maxShowBytes--;
            if (maxShowBytes <= 0) {
                break;
            }
        }
        if (idx != 0) {
            body.append("\r\n");
        }
        return body.toString();
    }
}

Related

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