Java Byte Array to String asString(byte[] a, String separator)

Here you can find the source of asString(byte[] a, String separator)

Description

Converts a array of numbers into a String.

License

LGPL

Parameter

Parameter Description
a the array to convert
separator string that separates each element

Return

a String representation of the array

Declaration

public static String asString(byte[] a, String separator) 

Method Source Code

//package com.java2s;
/*//  ww  w. ja  v  a  2 s . c o m
 * Copyright (c) 2013 mellowtech.org.
 *
 * The contents of this file are subject to the terms of one of the following
 * open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
 * 1.0 (the "Licenses"). You can select the license that you prefer but you may
 * not use this file except in compliance with one of these Licenses.
 *
 * You can obtain a copy of the Apache 2.0 license at
 * http://www.opensource.org/licenses/apache-2.0
 *
 * You can obtain a copy of the LGPL 3.0 license at
 * http://www.opensource.org/licenses/lgpl-3.0
 *
 * You can obtain a copy of the LGPL 2.1 license at
 * http://www.opensource.org/licenses/lgpl-2.1
 *
 * You can obtain a copy of the CDDL 1.0 license at
 * http://www.opensource.org/licenses/cddl1
 *
 * You can obtain a copy of the EPL 1.0 license at
 * http://www.opensource.org/licenses/eclipse-1.0
 *
 * See the Licenses for the specific language governing permissions and
 * limitations under the Licenses.
 */

public class Main {
    /**
     * Converts a array of numbers into a String.
     * 
     * @param a
     *          the array to convert
     * @param separator
     *          string that separates each element
     * @return a String representation of the array
     */
    public static String asString(byte[] a, String separator) {
        StringBuffer sbuff = new StringBuffer();
        for (int i = 0; i < a.length; i++)
            sbuff.append(a[i] + separator);
        sbuff.setLength(sbuff.length() - 1);
        return sbuff.toString();
    }

    /**
     * Converts a array of numbers into a String.
     * 
     * @param a
     *          the array to convert
     * @param separator
     *          string that separates each element
     * @return a String representation of the array
     */
    public static String asString(short[] a, String separator) {
        StringBuffer sbuff = new StringBuffer();
        for (int i = 0; i < a.length; i++)
            sbuff.append(a[i] + separator);
        sbuff.setLength(sbuff.length() - 1);
        return sbuff.toString();
    }

    /**
     * Converts a array of numbers into a String.
     * 
     * @param a
     *          the array to convert
     * @param separator
     *          string that separates each element
     * @return a String representation of the array
     */
    public static String asString(int[] a, String separator) {
        StringBuffer sbuff = new StringBuffer();
        for (int i = 0; i < a.length; i++)
            sbuff.append(a[i] + separator);
        sbuff.setLength(sbuff.length() - 1);
        return sbuff.toString();
    }

    /**
     * Converts a array of numbers into a String.
     * 
     * @param a
     *          the array to convert
     * @param separator
     *          string that separates each element
     * @return a String representation of the array
     */
    public static String asString(long[] a, String separator) {
        StringBuffer sbuff = new StringBuffer();
        for (int i = 0; i < a.length; i++)
            sbuff.append(a[i] + separator);
        sbuff.setLength(sbuff.length() - 1);
        return sbuff.toString();
    }

    /**
     * Converts a array of reals into a String.
     * 
     * @param a
     *          the array to convert
     * @param separator
     *          string that separates each element
     * @return a String representation of the array
     */
    public static String asString(float[] a, String separator) {
        StringBuffer sbuff = new StringBuffer();
        for (int i = 0; i < a.length; i++)
            sbuff.append(a[i] + separator);
        sbuff.setLength(sbuff.length() - 1);
        return sbuff.toString();
    }

    /**
     * Converts a array of reals into a String.
     * 
     * @param a
     *          the array to convert
     * @param separator
     *          string that separates each element
     * @return a String representation of the array
     */
    public static String asString(double[] a, String separator) {
        StringBuffer sbuff = new StringBuffer();
        for (int i = 0; i < a.length; i++)
            sbuff.append(a[i] + separator);
        sbuff.setLength(sbuff.length() - 1);
        return sbuff.toString();
    }

    /**
     * Converts an array of Objects into a String.
     * 
     * @param a
     *          the array to convert
     * @param separator
     *          string that separates each element
     * @return a String representation of the array
     */
    public static String asString(Object[] a, String separator) {
        StringBuffer sbuff = new StringBuffer();
        for (int i = 0; i < a.length; i++)
            sbuff.append(a[i] + separator);
        sbuff.setLength(sbuff.length() - 1);
        return sbuff.toString();
    }
}

Related

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