convert from internal Java String format -> UTF-8 - Java java.lang

Java examples for java.lang:String UTF

Description

convert from internal Java String format -> UTF-8

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String s = "java2s.com";
        System.out.println(convertToUTF8(s));
    }/*from   w  w w . ja va  2s  .  co m*/

    /**
     * convert from internal Java String format -> UTF-8
     * @param s the string
     * @return the UTF-8 representation.
     */
    public static String convertToUTF8(String s) {
        String out = null;
        try {
            out = new String(s.getBytes("UTF-8"), "ISO-8859-1");
        } catch (java.io.UnsupportedEncodingException e) {
            return null;
        }
        return out;
    }
}

Related Tutorials