Java Byte Array Convert To bytes_to_str(byte[] data)

Here you can find the source of bytes_to_str(byte[] data)

Description

bytetstr

License

Open Source License

Declaration

public static String bytes_to_str(byte[] data) 

Method Source Code

//package com.java2s;
/*/* w w  w.j  av  a 2  s  .  c o  m*/
 * Copyright (c) 2001-2003 Regents of the University of California.
 * All rights reserved.
 *
 * See the file LICENSE included in this distribution for details.
 */

public class Main {
    private static final char[] digits = { '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    public static String bytes_to_str(byte[] data) {
        return bytes_to_str(data, 0, data.length, false);
    }

    public static String bytes_to_str(byte[] data, int offset, int length) {
        return bytes_to_str(data, offset, length, false);
    }

    public static String bytes_to_str(byte[] data, int offset, int length,
            boolean pretty) {
        StringBuffer sbuf = new StringBuffer(length * 2);
        bytes_to_sbuf(data, offset, length, pretty, sbuf);
        return sbuf.toString();
    }

    public static void bytes_to_sbuf(byte[] data, int offset, int length,
            StringBuffer buf) {
        bytes_to_sbuf(data, offset, length, false, buf);
    }

    public static void bytes_to_sbuf(byte[] data, int offset, int length,
            boolean pretty, StringBuffer buf) {
        int size = 2 * length;
        size += (size / 8) + (size / 64);
        int low_mask = 0x0f;
        int high_mask = 0xf0;
        byte b;

        int j = 0;
        for (int i = offset; i < (offset + length); ++i) {
            b = data[i];
            buf.append(digits[(high_mask & b) >> 4]);
            buf.append(digits[(low_mask & b)]);
            if (pretty) {
                if (j % 4 == 3)
                    buf.append(' ');
                if (j % 32 == 31)
                    buf.append('\n');
            }
            ++j;
        }
    }
}

Related

  1. bytes2unique(byte[] daten, int offset)
  2. Bytes4ToInt(byte abyte0[], int offset)
  3. Bytes8ToLong(byte abyte0[], int offset)
  4. bytes_to_sbuf(byte[] data, int offset, int length, StringBuffer buf)
  5. bytes_to_short(byte[] buf, int offset)
  6. bytesToAlphaNumeric(byte[] number)
  7. bytesToAsciiMaybe(byte[] data)
  8. bytesToBlocks(final long bytes, final int blockSize)
  9. bytesToBool(byte[] data, int[] offset)