String to UTF-16BE - Java Internationalization

Java examples for Internationalization:Charset

Description

String to UTF-16BE

Demo Code


//package com.java2s;
import java.io.UnsupportedEncodingException;

public class Main {
    public static void main(String[] argv) throws Exception {
        String str = "java2s.com";
        System.out.println(toUTF_16BE(str));
    }/*from w  w  w.  jav a  2 s  .c  o  m*/

    public static final String UTF_16BE = "UTF-16BE";

    public static String toUTF_16BE(String str)
            throws UnsupportedEncodingException {
        return changeCharset(str, UTF_16BE);
    }

    public static String changeCharset(String str, String newCharset)
            throws UnsupportedEncodingException {
        if (str != null) {
            // ?windows?GB2312
            byte[] bs = str.getBytes();
            return new String(bs, newCharset); // 
        }
        return null;
    }

    public static String changeCharset(String str, String oldCharset,
            String newCharset) throws UnsupportedEncodingException {
        if (str != null) {
            // 
            byte[] bs = str.getBytes(oldCharset);
            return new String(bs, newCharset);
        }
        return null;
    }
}

Related Tutorials