convert byte array To String via Charset - Java Internationalization

Java examples for Internationalization:Charset

Description

convert byte array To String via Charset

Demo Code


//package com.java2s;

import java.nio.charset.*;

public class Main {
    public static String convertToString(byte[] bytes, Charset charset) {
        String ret = new String(bytes, charset);
        if ((bytes[0] == 0xEF - 256) && (bytes[1] == 0xBB - 256)
                && (bytes[2] == 0xBF - 256)) {
            ret = ret.substring(1);/*from   w  w  w.  j  a  v a 2  s. co  m*/
        } else if ((bytes[0] == 0xFE - 256) && (bytes[1] == 0xFF - 256)) {
            ret = ret.substring(1);
        } else if ((bytes[0] == 0xFF - 256) && (bytes[1] == 0xFE - 256)) {
            ret = ret.substring(1);
        }
        return ret;
    }
}

Related Tutorials