Java Utililty Methods UTF8 Encode

List of utility methods to do UTF8 Encode

Description

The list of methods to do UTF8 Encode are organized into topic(s).

Method

StringtoUTF8(String isoString)
Convert ISO8859-1 format string (which is the default sent by IE to the UTF-8 format that the database is in.
String utf8String = null;
if (null != isoString && !isoString.equals("")) {
    try {
        byte[] stringBytesISO = isoString.getBytes("ISO-8859-1");
        utf8String = new String(stringBytesISO, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        System.out.println("UnsupportedEncodingException is: " + e.getMessage());
        utf8String = isoString;
...
StringtoUTF8(String s)
Encodes a string to UTF8
if (s != null) {
    try {
        return new String(s.getBytes("UTF8"));
    } catch (UnsupportedEncodingException e) {
return null;
StringtoUTF8(String sourceStr)
to UTF
return changeEncoding(sourceStr, "ISO8859-1", "UTF-8");
StringtoUTF8(String str)
to UTF
try {
    return URLEncoder.encode(str, "UTF-8").replace("%2F", "/").replace("+", " ");
} catch (UnsupportedEncodingException ex) {
    return str;
byte[]toUTF8(String str)
Converts the string to a UTF-8 byte array, turning the checked exception (which should never happen) into a runtime exception.
try {
    if (str == null)
        return new byte[0];
    return str.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException("UTF-8 not supported", e);
StringtoUtf8(String str)
to Utf
String val = "";
try {
    val = new String(str.getBytes("UTF-8"), "UTF-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
return val;
StringtoUTF8(String str)
to UTF
if (!isNull(str)) {
    try {
        str = new String(str.getBytes("ISO8859-1"), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
return str;
...
byte[]toUTF8(String string)
Convert from a String to UTF-8 bytes.
if (string == null) {
    return null;
try {
    return string.getBytes(UTF_8_ENCODING);
} catch (UnsupportedEncodingException e) {
    throw new IllegalStateException("UTF-8 not supported?", e);
StringtoUTF8(String value)
to UTF
if (value == null) {
    return "";
byte[] b = value.getBytes("ISO-8859-1");
return new String(b, "utf-8");
byte[]toUTF8ByteArray(char[] string)
to UTF Byte Array
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
try {
    toUTF8ByteArray(string, bOut);
} catch (IOException e) {
    throw new IllegalStateException("cannot encode string to byte array!");
return bOut.toByteArray();