Java Byte Array to String by Charset toString(byte[] data, int from, int length, Charset charset)

Here you can find the source of toString(byte[] data, int from, int length, Charset charset)

Description

Convert to string with UTF8 charset.

License

Apache License

Parameter

Parameter Description
data Value.
from From index.
length Byte count.
charset Charset

Return

Result.

Declaration

public static String toString(byte[] data, int from, int length, Charset charset) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2017 UIA/* w  w  w. j a  v a2s .co m*/
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 *******************************************************************************/

import java.nio.charset.Charset;
import java.util.Arrays;

public class Main {
    /**
     * Convert to string with UTF8 charset.
     * @param data Value.
     * @param from From index.
     * @param length Byte count.
     * @return Result.
     */
    public static String toString(byte[] data, int from, int length) {
        return new String(Arrays.copyOfRange(data, from, from + length));
    }

    /**
     * Convert to string with UTF8 charset.
     * @param data Value.
     * @param from From index.
     * @param length Byte count.
     * @param charset Charset
     * @return Result.
     */
    public static String toString(byte[] data, int from, int length, Charset charset) {
        return new String(Arrays.copyOfRange(data, from, from + length), charset);
    }
}

Related

  1. toDebugString(final byte[] bytes, final int len, final Charset charset)
  2. toEncodedString(byte[] bytes, Charset charset)
  3. toString(byte[] b, String charset)
  4. toString(byte[] bytes, Charset charset)
  5. toString(byte[] bytes, String charsetName)
  6. toString(final byte[] bytes, final String charsetName)
  7. toUnicode(byte[] isoStr, String charset)
  8. truncateByByteLength(String string, int maxBytes, Charset charset)
  9. yaml2List(Charset charset, byte[] data)