Java UTF8 codeToString(int utf8Code)

Here you can find the source of codeToString(int utf8Code)

Description

convert utf8 code to string

License

Apache License

Parameter

Parameter Description
utf8Code a parameter

Declaration

public static String codeToString(int utf8Code) 

Method Source Code


//package com.java2s;
/*//ww w  .  jav  a 2s.  c  o  m
 * Copyright (C) 2014-2015 Nagisa Sekiguchi
 *
 * 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.
 */

import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public final static Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

    /**
     * convert utf8 code to string
     *
     * @param utf8Code
     * @return
     */
    public static String codeToString(int utf8Code) {
        return codesToString(new int[] { utf8Code });
    }

    /**
     * convert utf8 code to string and write buffer.
     *
     * @param buffer
     * @param utf8Code
     */
    private static void codeToString(ByteArrayOutputStream buffer, int utf8Code) {
        for (int i = 3; i > -1; i--) {
            int shift = i * 8;
            int mask = 0xff << shift;
            byte result = (byte) ((utf8Code & mask) >> shift);
            if (result != 0) {
                buffer.write(result);
            }
        }
    }

    /**
     * convert utf8 codes to string
     *
     * @param utf8Codes
     * @return
     */
    public static String codesToString(int[] utf8Codes) {
        List<Integer> codeList = new ArrayList<>(utf8Codes.length);
        for (int code : utf8Codes) {
            codeList.add(code);
        }
        return codeListToString(codeList);
    }

    /**
     * convert utf8 code list to string
     *
     * @param utf8CodeList
     * @return
     */
    public static String codeListToString(List<Integer> utf8CodeList) {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        for (int code : utf8CodeList) {
            codeToString(buffer, code);
        }
        return new String(buffer.toByteArray(), DEFAULT_CHARSET);
    }
}

Related

  1. asUTF8String(byte[] bytes)
  2. asUTFString(byte[] content)
  3. backup(String inputFile, String backupFile)
  4. codeListToString(List utf8CodeList)
  5. codesToString(int[] utf8Codes)
  6. compareWithGold(String goldfile, String outputfile)
  7. convertISO8859_1_to_UTF_8(String s)
  8. ConvertUTF8(byte[] buffer)
  9. createFileAndExport(String inputFileName, String outputFileName, String outputStr)