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

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

Description

Return the String representation of the byte array.

License

Apache License

Parameter

Parameter Description
bytes the byte array

Return

its string representation

Declaration

public static String asString(byte[] bytes) 

Method Source Code

//package com.java2s;
/**/*w  ww.  j  a  va2 s  .  co  m*/
 *
 * jerry - Common Java Functionality
 * Copyright (c) 2012-2015, Sandeep Gupta
 * 
 * http://sangupta.com/projects/jerry
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

public class Main {
    /**
     * An empty string object containing nothing.
     */
    public static final String EMPTY_STRING = "";

    /**
     * Return the {@link String} representation of the byte array. A
     * <code>null</code> byte array is converted back to <code>null</code>, an
     * empty array to an empty string or the bytes to the actual string based on
     * platform encoding.
     * 
     * @param bytes
     *            the byte array
     * 
     * @return its string representation
     */
    public static String asString(byte[] bytes) {
        if (bytes == null) {
            return null;
        }

        if (bytes.length == 0) {
            return EMPTY_STRING;
        }

        return new String(bytes);
    }
}

Related

  1. asString(byte[] a, String separator)
  2. asString(byte[] array)
  3. asString(byte[] bs)
  4. asString(byte[] buf)
  5. asString(byte[] bytes)
  6. ByteArrayToString(byte[] byteArray)
  7. bytes2str(byte[] arr)
  8. bytes2Str(byte[] bytes, String separator)
  9. bytes2String(byte[] args)