Java Utililty Methods String Encode

List of utility methods to do String Encode

Description

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

Method

byte[]encodeString(String s)
encode String
try {
    return s.getBytes("MACROMAN");
} catch (UnsupportedEncodingException uee) {
    ByteArrayOutputStream bs = new ByteArrayOutputStream(s.length());
    for (char ch : s.toCharArray()) {
        if (ch < 0x80)
            bs.write(ch);
        else {
...
byte[]encodeString(String s, boolean useUnicode)
Encodes the string to a byte array using UTF-16LE or the ASCII charset in function of the useUnicode argument.
if (useUnicode) {
    return getUTFStringAsByteArray(s);
return getOEMStringAsByteArray(s);
byte[]encodeString(String s, Integer size)
encode String
byte[] result = null;
try {
    result = s.getBytes("ASCII");
} catch (java.io.UnsupportedEncodingException e) {
    result = s.getBytes();
return (size == null) ? result : alignToSize(result, size);
StringencodeString(String s, String charset)
Encodes a string into Base64 format.
return new String(encode(s.getBytes(charset)));
StringencodeString(String sourceString, String sysCharset, String charset)
encode String
if (sourceString == null || sourceString.length() == 0)
    return sourceString;
return new String(sourceString.getBytes(sysCharset), "GB2312");
StringencodeString(String str)
Encode a string using Base64 encoding.
BASE64Encoder encoder = new BASE64Encoder();
String encodedStr = encoder.encodeBuffer(str.getBytes());
return (encodedStr.trim());
StringencodeString(String strData)
encode String to Base64
if (strData == null)
    return "";
try {
    byte[] bytes = strData.getBytes("UTF8");
    return encode(bytes).replace("\n", "");
} catch (IOException ie) {
    ie.printStackTrace();
return "";
StringencodeString(String text, String charsetName)
encode String
if (text != null) {
    byte bbb[] = text.getBytes();
    try {
        return new String(bbb, charsetName);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    return text;
...
StringencodeString(String value)
Base64 encodes the specified string.
ByteArrayInputStream in = new ByteArrayInputStream(value.getBytes(ENCODING));
ByteArrayOutputStream out = new ByteArrayOutputStream();
encode(in, out);
return out.toString();
StringencodeStringByUTF8(String str)
encode String By UTF
if (isBlank(str))
    return "";
try {
    return URLEncoder.encode(str, "utf-8");
} catch (UnsupportedEncodingException e) {
return "";