Android Utililty Methods Byte Array to String Convert

List of utility methods to do Byte Array to String Convert

Description

The list of methods to do Byte Array to String Convert are organized into topic(s).

Method

Stringbytes2StringPtr(byte[] data, String type)
bytes String Ptr
if (type == null) {
    type = "";
StringBuffer sb = new StringBuffer();
System.out.println();
for (int i = 0; i < data.length; i++) {
    sb.append(String.format("".equals(type) ? "0x%x," : type + ",",
            data[i]));
...
StringgetString(byte[] bb)
get String
return new String(bb);
StringgetString(byte[] bytes)
get String
return getString(bytes, "GBK");
StringgetString(byte[] bytes, String charsetName)
get String
return new String(bytes, Charset.forName(charsetName));
StringgetString(byte[] fromBytes, int offset, int length)
get String
final String toString;
try {
    toString = new String(fromBytes, offset, length, BYTE_CHARSET);
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException(e);
return toString;
StringgetString(byte[] originalByte, int start, int length)
get String
byte[] strByte = Arrays.copyOfRange(originalByte, start, start
        + length);
return new String(strByte);
StringgetString(byte[] value)
get String
StringBuilder stringBuilder = new StringBuilder("");
for (int i = 0; i < value.length; i++) {
    int v = value[i] & 0xFF;
    String hv = Integer.toHexString(v);
    if (hv.length() < 2) {
        stringBuilder.append(0);
    stringBuilder.append(hv);
...
StringgetStringByByteArray(byte[] b)
get String By Byte Array
return new String(b);
StringtoByteString(byte[] bytes)
to Byte String
StringBuilder byteString = new StringBuilder();
for (byte b : bytes) {
    if (b >= 32 && b <= 127) {
        byteString.append((char) b);
    } else {
        byteString.append(String.format("\\x%02x", b));
return byteString.toString();
StringtoByteString(byte[] bytes, int start, int length)
to Byte String
StringBuilder byteString = new StringBuilder();
int maxlength = bytes.length < start + length ? bytes.length
        : start + length;
for (int i = start; i < maxlength; i++) {
    if (bytes[i] >= 32 && bytes[i] <= 127) {
        byteString.append((char) bytes[i]);
    } else {
        byteString.append(String.format("\\x%02x", bytes[i]));
...