Java Array Dump dumpString(byte[] b)

Here you can find the source of dumpString(byte[] b)

Description

converts a byte array to printable characters

License

Open Source License

Parameter

Parameter Description
b - byte array

Return

String representation

Declaration

public static String dumpString(byte[] b) 

Method Source Code

//package com.java2s;
/*//from w w w . j a  v a 2s . c  om
 * jPOS Project [http://jpos.org]
 * Copyright (C) 2000-2008 Alejandro P. Revilla
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * converts a byte array to printable characters
     * 
     * @param b
     *            - byte array
     * @return String representation
     */
    public static String dumpString(byte[] b) {
        StringBuffer d = new StringBuffer(b.length * 2);
        for (int i = 0; i < b.length; i++) {
            char c = (char) b[i];
            if (Character.isISOControl(c)) {
                // TODO: complete list of control characters,
                // use a String[] instead of this weird switch
                switch (c) {
                case '\r':
                    d.append("{CR}");
                    break;
                case '\n':
                    d.append("{LF}");
                    break;
                case '\000':
                    d.append("{NULL}");
                    break;
                case '\001':
                    d.append("{SOH}");
                    break;
                case '\002':
                    d.append("{STX}");
                    break;
                case '\003':
                    d.append("{ETX}");
                    break;
                case '\004':
                    d.append("{EOT}");
                    break;
                case '\005':
                    d.append("{ENQ}");
                    break;
                case '\006':
                    d.append("{ACK}");
                    break;
                case '\007':
                    d.append("{BEL}");
                    break;
                case '\020':
                    d.append("{DLE}");
                    break;
                case '\025':
                    d.append("{NAK}");
                    break;
                case '\026':
                    d.append("{SYN}");
                    break;
                case '\034':
                    d.append("{FS}");
                    break;
                case '\036':
                    d.append("{RS}");
                    break;
                default:
                    char hi = Character.forDigit((b[i] >> 4) & 0x0F, 16);
                    char lo = Character.forDigit(b[i] & 0x0F, 16);
                    d.append('[');
                    d.append(Character.toUpperCase(hi));
                    d.append(Character.toUpperCase(lo));
                    d.append(']');
                    break;
                }
            } else
                d.append(c);

        }
        return d.toString();
    }
}

Related

  1. dumpMatrix(byte[] matrix, int numDataUnits, int numAllUnits)
  2. dumpMessage(byte b[])
  3. dumpOctets(byte[] data)
  4. dumpPosition(long[] position)
  5. dumpStackTrace(StackTraceElement[] elements)
  6. dumpString(byte[] b)
  7. dumpString(byte[] data)
  8. dumpString(byte[] data)
  9. dumpString(byte[] data)