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

StringtoAsciiString(byte[] raw)
Converts an array of raw binary data into a String of ascii 0 and 1 characters.
return new String(toAsciiChars(raw));
StringtoString(byte[] array)
Converts a byte array into its string representation.
return toString(array, null, 0);
StringtoString(byte[] array, String separator, int frequency)
Covert each byte of the array in a two characters string representation.
int limit = arrays.length(array);
int extra = (separator == null ? 0 : separator.length() * limit);
StringBuilder sb = new StringBuilder(limit * 2 + extra);
if (separator != null) {
    for (int i = 0; i < limit; i++) {
        if ((i > 0) && (((i + 1) % frequency) == 0))
            sb.append(separator);
        sb.append(String.format("%02X", array[i]));
...
StringtoString(byte[] b)
Convert binary data to a hex-encoded String
ByteArrayOutputStream os = new ByteArrayOutputStream();
for (int i = 0; i < b.length; i++) {
    short value = (short) (b[i] & 0xFF);
    byte high = (byte) (value >> 4);
    byte low = (byte) (value & 0xF);
    os.write(Base16.charAt(high));
    os.write(Base16.charAt(low));
return new String(os.toByteArray());
StringtoString(byte[] theByteArray)
to String
StringBuffer out = new StringBuffer();
for (int i = 0; i < theByteArray.length; i++) {
    String s = Integer.toHexString(theByteArray[i] & 0xff);
    if (s.length() < 2) {
        out.append('0');
    out.append(s).append(' ');
return out.toString();
StringfromBytes(byte[] buf)
Return a new String with chars corresponding to buf.
return fromBytes(buf, 0, buf.length);
StringfromBytes(byte[] buf, int off, int len)
Return a new String with chars corresponding to buf from off to off + len.
return new String(buf, 0, off, len);
Stringbytes2String(byte[] value)
bytes String
return (value == null) ? "" : new String(value);
Stringbyte2String(byte[] is)
byte String
String srt = new String(is, "UTF-8");
return srt;
StringloadConvert(byte[] s, int offset, boolean lengthFlag)
load Convert
if (null == s || (offset + (lengthFlag ? 1 : 0)) > s.length)
    throw new IllegalArgumentException("invalid byte arrary");
char aChar;
int len = (s.length - offset);
if (lengthFlag) {
    len = s[offset] & 0xFF;
    offset++;
StringBuffer outBuffer = new StringBuffer(len);
for (int x = offset; x < (offset + len);) {
    aChar = (char) s[x++];
    if (aChar == '\\') {
        aChar = (char) s[x++];
        if (aChar == 'u') {
            int value = 0;
            for (int i = 0; i < 4; i++) {
                aChar = (char) s[x++];
                switch (aChar) {
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                    value = (value << 4) + aChar - '0';
                    break;
                case 'a':
                case 'b':
                case 'c':
                case 'd':
                case 'e':
                case 'f':
                    value = (value << 4) + 10 + aChar - 'a';
                    break;
                case 'A':
                case 'B':
                case 'C':
                case 'D':
                case 'E':
                case 'F':
                    value = (value << 4) + 10 + aChar - 'A';
                    break;
                default:
                    throw new IllegalArgumentException(
                            "Malformed \\uxxxx encoding.");
            outBuffer.append((char) value);
        } else {
            if (aChar == 't')
                aChar = '\t';
            else if (aChar == 'r')
                aChar = '\r';
            else if (aChar == 'n')
                aChar = '\n';
            else if (aChar == 'f')
                aChar = '\f';
            outBuffer.append(aChar);
    } else
        outBuffer.append(aChar);
return outBuffer.toString();