Java Utililty Methods Unicode Create

List of utility methods to do Unicode Create

Description

The list of methods to do Unicode Create are organized into topic(s).

Method

byte[]getUnicodeByteArray(String str)
get Unicode Byte Array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
    for (int i = 0; i < str.length(); i++) {
        dos.writeChar((int) str.charAt(i));
} catch (Exception e) {
return baos.toByteArray();
byte[]getUnicodeBytes(String s)
Converts the string into a little-endian array of Unicode bytes
try {
    byte[] b = s.getBytes(UNICODE_ENCODING);
    if (b.length == (s.length() * 2 + 2)) {
        byte[] b2 = new byte[b.length - 2];
        System.arraycopy(b, 2, b2, 0, b2.length);
        b = b2;
    return b;
...
StringgetUnicodeString(byte bytes[], int offset, int len)
Get UNICODE String.
try {
    return new String(bytes, offset, len * 2, UNICODE_ENCODING);
} catch (UnsupportedEncodingException e) {
    return "";
StringgetUnicodeString(byte[] d, int length, int pos)
Gets a string from the data array
try {
    byte[] b = new byte[length * 2];
    System.arraycopy(d, pos, b, 0, length * 2);
    return new String(b, UNICODE_ENCODING);
} catch (UnsupportedEncodingException e) {
    return "";
StringgetUnicodeText(Object text)
get Unicode Text
if (text == null) {
    return null;
} else if (text instanceof byte[]) {
    return new String((byte[]) text, "UTF-8");
} else {
    return String.valueOf(text);
StringtoUnicode(char c)
to Unicode
return toUnicode(String.valueOf(c));
StringtoUnicode(final String toUnicode, final boolean toLowerCase)
Converts all characters from the given String to unicodes characters encoded like \uxxxx.
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < toUnicode.length(); i++) {
    final String hex = Integer.toHexString(toUnicode.codePointAt(i));
    if (toLowerCase) {
        hex.toLowerCase();
    } else {
        hex.toUpperCase();
    final String hexWithZeros = "0000" + hex;
    final String hexCodeWithLeadingZeros = hexWithZeros.substring(hexWithZeros.length() - 4);
    sb.append("\\u" + hexCodeWithLeadingZeros);
return sb.toString();
StringtoUnicode(String input)
to Unicode
StringBuilder ret = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
    char ch = input.charAt(i);
    if (!Character.isWhitespace(ch) && ch < 0x20 || ch > 0x7e) {
        ret.append("\\u");
        ret.append(leading4Zeros(Integer.toHexString(ch)));
    } else {
        ret.append(ch);
...
StringtoUnicode(String input)
to Unicode
String output = "";
try {
    if (input != null && input.length() > 0) {
        for (int i = 0; i < input.length(); i++) {
            output += toUnicode(input.charAt(i));
} catch (Exception e) {
...
StringtoUnicode(String input)
to Unicode
StringBuffer ret = new StringBuffer();
for (int i = 0; i < input.length(); i++) {
    char ch = input.charAt(i);
    if ((!Character.isWhitespace(ch) && ch < 0x20 || ch > 0x7e)) {
        ret.append("\\u");
        ret.append(fillChar(Integer.toHexString(ch), '0', 4, true));
    } else {
        ret.append(ch);
...