Java Byte Array to String asString(byte[] array)

Here you can find the source of asString(byte[] array)

Description

Converts the given byte array to a string using a default separator character.

License

Open Source License

Parameter

Parameter Description
array array to convert

Return

the converted string

Declaration

public static String asString(byte[] array) 

Method Source Code

//package com.java2s;
/*/*from   ww  w  . j av a  2  s  .co  m*/
 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Sly Technologies, Inc.
 *
 * This file is part of jNetPcap.
 *
 * jNetPcap is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Converts the given byte array to a string using a default separator
     * character.
     * 
     * @param array
     *            array to convert
     * @return the converted string
     */
    public static String asString(byte[] array) {
        return asString(array, ':');
    }

    /**
     * Convers the given byte array to a string using the supplied separator
     * character.
     * 
     * @param array
     *            array to convert
     * @param separator
     *            separator character to use in between array elements
     * @return the converted string
     */
    public static String asString(byte[] array, char separator) {
        return asString(array, separator, 16); // Default HEX
    }

    /**
     * Converts the given byte array to a string using the supplied separator
     * character and radix for conversion of the numerical component.
     * 
     * @param array
     *            array to convert
     * @param separator
     *            separator character to use in between array elements
     * @param radix
     *            numerical radix to use for numbers
     * @return the converted string
     */
    public static String asString(byte[] array, char separator, int radix) {
        return asString(array, separator, radix, 0, array.length);
    }

    /**
     * Convers the given byte array to a string using the supplied separator
     * character.
     * 
     * @param array
     *            array to convert
     * @param separator
     *            separator character to use in between array elements
     * @param radix
     *            the radix
     * @param start
     *            the start
     * @param len
     *            the len
     * @return the converted string
     */
    public static String asString(byte[] array, char separator, int radix, int start, int len) {

        final StringBuilder buf = new StringBuilder();

        for (int i = start; i < (start + len); i++) {
            byte b = array[i];
            if (buf.length() != 0) {
                buf.append(separator);
            }

            buf.append(Integer.toString((b < 0) ? b + 256 : b, radix).toUpperCase());
        }

        return buf.toString();
    }
}

Related

  1. arr2String(byte[] arr, int start)
  2. ASCIIByteArrayToString(byte[] data)
  3. asString(byte[] a, String separator)
  4. asString(byte[] bs)
  5. asString(byte[] buf)
  6. asString(byte[] bytes)
  7. asString(byte[] bytes)