Java Utililty Methods UTF from

List of utility methods to do UTF from

Description

The list of methods to do UTF from are organized into topic(s).

Method

StringtoUTF8(String s, String encoding)
to UTF
if (s != null && s.length() > 0) {
    byte[] byteTmp = s.getBytes(encoding);
    s = new String(byteTmp, "UTF-8");
return s;
StringtoUTF8(String str)
to UTF
if (str != null && !str.equals("")) {
    try {
        return new String(str.getBytes("ISO8859-1"), "UTF-8");
    } catch (Exception var2) {
        var2.printStackTrace();
        return "";
} else {
...
byte[]toUTF8(String string)
Encodes a String to UTF-8.
return string.getBytes(UTF_8);
StringtoUtf8(String texto)
to Utf
try {
    return new String(texto.getBytes(), "UTF-8");
} catch (Exception e) {
    return "";
byte[]toUTF8ByteArray(String s)
convert the platform dependent string characters to UTF8 which can also be done by calling the java String method getBytes("UTF-8"),but I hope to do it from the ground up.
int ichar;
byte buffer[] = new byte[3 * (s.length())];
byte hold[];
int index = 0;
int count = 0; 
for (int i = 0; i < s.length(); i++) {
    ichar = (int) s.charAt(i);
    if ((ichar >= 0x0080) & (ichar <= 0x07FF)) {
...
byte[]toUtf8ByteArray(String source)
Converts a String to a UTF-8 byte array
byte[] bytes = new byte[source.length()];
byte[] four = new byte[4];
for (int i = 0; i < source.length(); i++) {
    intToByteArray(four, (int) source.charAt(i));
    if (four[2] > 0) {
        throw new RuntimeException("Encoding not valid for string. Try UTF-8");
    bytes[i] = four[3];
...
inttoUtf8Code(char utf16)
convert java character literal to utf8 code
byte[] buf = Character.toString(utf16).getBytes(DEFAULT_CHARSET);
return toUtf8Code(buf, 0, buf.length);
int[]toUtf8Codes(String str)
convert string to utf8 codes
byte[] buf = str.getBytes(DEFAULT_CHARSET);
int startIndex = 0;
List<Integer> utf8CodeList = new ArrayList<>();
while (startIndex < buf.length) {
    byte b = buf[startIndex];
    int charLength = getUtf8Length(b);
    utf8CodeList.add(toUtf8Code(buf, startIndex, charLength));
    startIndex += charLength;
...
inttoUTF8FromLatin1(byte[] outputBuffer, String string)
Encode the string into UTF8, assume the original string is ISO-8859-1
if (string == null)
    return 0;
int len = string.length();
for (int i = 0; i < string.length(); i++) {
    outputBuffer[i] = (byte) string.charAt(i);
return len;
StringtoUTF8k(String in)
to UT Fk
String utf = in;
try {
    if (in != null)
        utf = new String(in.getBytes("EUC-KR"), "UTF-8");
} catch (Exception e) {
return utf;