Android Byte Array to String Convert fromBytes(byte[] buf)

Here you can find the source of fromBytes(byte[] buf)

Description

Return a new String with chars corresponding to buf.

License

Open Source License

Parameter

Parameter Description
buf an array of bytes

Return

a new String corresponding to the bytes in buf

Declaration

public static String fromBytes(byte[] buf) 

Method Source Code

//package com.java2s;

import java.nio.ByteBuffer;

public class Main {
    /**/*from   w  w  w  . j  a va2s  .c o m*/
     * Return a new String with chars corresponding to buf from off to
     * off + len.
     *
     * @param buf an array of bytes
     * @param off the initial offset
     * @param len the length
     * @return a new String corresponding to the bytes in buf
     */
    @SuppressWarnings("deprecation")
    public static String fromBytes(byte[] buf, int off, int len) {
        // Yes, I known the method is deprecated, but it is the fastest
        // way of converting between between byte[] and String
        return new String(buf, 0, off, len);
    }

    /**
     * Return a new String with chars corresponding to buf.
     *
     * @param buf an array of bytes
     * @return a new String corresponding to the bytes in buf
     */
    public static String fromBytes(byte[] buf) {
        return fromBytes(buf, 0, buf.length);
    }

    /**
     * Return a new String with chars corresponding to buf.
     *
     * @param buf a ByteBuffer of bytes
     * @return a new String corresponding to the bytes in buf
     */
    public static String fromBytes(ByteBuffer buf) {
        return fromBytes(buf.array(), buf.arrayOffset() + buf.position(),
                buf.arrayOffset() + buf.limit());
    }
}

Related

  1. toAsciiString(byte[] raw)
  2. toString(byte[] array)
  3. toString(byte[] array, String separator, int frequency)
  4. toString(byte[] b)
  5. toString(byte[] theByteArray)
  6. fromBytes(byte[] buf, int off, int len)
  7. bytes2String(byte[] value)
  8. byte2String(byte[] is)
  9. loadConvert(byte[] s, int offset, boolean lengthFlag)