Android Unicode to String Convert getFromCompressedUnicode(final byte[] string, final int offset, final int len)

Here you can find the source of getFromCompressedUnicode(final byte[] string, final int offset, final int len)

Description

Read 8 bit data (in ISO-8859-1 codepage) into a (unicode) Java String and return.

License

Apache License

Parameter

Parameter Description
string byte array to read
offset offset to read byte array
len length to read byte array

Return

String generated String instance by reading byte array

Declaration

public static String getFromCompressedUnicode(final byte[] string,
        final int offset, final int len) 

Method Source Code

//package com.java2s;
/* ====================================================================
 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./*from w ww.j a v a2  s  .c  o  m*/
 ==================================================================== */

import java.io.UnsupportedEncodingException;

public class Main {
    private static final String ENCODING_ISO_8859_1 = "ISO-8859-1";

    /**
     * Read 8 bit data (in ISO-8859-1 codepage) into a (unicode) Java
     * String and return.
     * (In Excel terms, read compressed 8 bit unicode as a string)
     *
     * @param string byte array to read
     * @param offset offset to read byte array
     * @param len    length to read byte array
     * @return String generated String instance by reading byte array
     */
    public static String getFromCompressedUnicode(final byte[] string,
            final int offset, final int len) {
        try {
            int len_to_use = Math.min(len, string.length - offset);
            return new String(string, offset, len_to_use,
                    ENCODING_ISO_8859_1);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. unUnicode(String s)
  2. unicodeToString(String hex)