Android Byte Array to String Convert convertBytesToString(byte[] value, int len)

Here you can find the source of convertBytesToString(byte[] value, int len)

Description

Convert a byte array to a hex encoded string.

License

Open Source License

Parameter

Parameter Description
value the byte array
len the number of bytes to encode

Return

the hex encoded string

Declaration

public static String convertBytesToString(byte[] value, int len) 

Method Source Code

//package com.java2s;
/*//from   w  w  w. j  a  v a2  s.  c  o m
 * Copyright 2004-2008 H2 Group. Multiple-Licensed under the H2 License, Version 1.0, and under the Eclipse Public License, Version 1.0 (http://h2database.com/html/license.html). Initial Developer: H2 Group
 */

public class Main {
    private static final char[] HEX = "0123456789abcdef".toCharArray();

    /**
     * Convert a byte array to a hex encoded string.
     * 
     * @param value
     *          the byte array
     * @return the hex encoded string
     */
    public static String convertBytesToString(byte[] value) {
        return convertBytesToString(value, value.length);
    }

    /**
     * Convert a byte array to a hex encoded string.
     * 
     * @param value
     *          the byte array
     * @param len
     *          the number of bytes to encode
     * @return the hex encoded string
     */
    public static String convertBytesToString(byte[] value, int len) {
        char[] buff = new char[len + len];
        char[] hex = HEX;
        for (int i = 0; i < len; i++) {
            int c = value[i] & 0xff;
            buff[i + i] = hex[c >> 4];
            buff[i + i + 1] = hex[c & 0xf];
        }
        return new String(buff);
    }
}

Related

  1. getString(byte[] value)
  2. getStringByByteArray(byte[] b)
  3. toByteString(byte[] bytes)
  4. toByteString(byte[] bytes, int start, int length)
  5. convertBytesToString(byte[] value)
  6. byteTOString(byte[] in)
  7. byteTOString(byte[] in)
  8. byteTOString(byte[] in)
  9. byteTOString(byte[] in)