Java Char Convert To convertCharsToUnicodeChars(final String s, final boolean toLowerCase)

Here you can find the source of convertCharsToUnicodeChars(final String s, final boolean toLowerCase)

Description

Converts all characters from the given String to unicodes characters encoded like \uxxxx.

License

Apache License

Parameter

Parameter Description
s The String to convert.
toLowerCase If true the letters from the unicode characters are lower case.

Return

The converted String.

Declaration

public static String convertCharsToUnicodeChars(final String s, final boolean toLowerCase) 

Method Source Code

//package com.java2s;
/**/*from   w w w . j  ava  2 s . co m*/
 * Copyright (C) 2007 Asterios Raptis
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /** A char array from the hexadecimal digits. */
    private static final char[] HEXADECIMAL_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
            'C', 'D', 'E', 'F' };

    /**
     * Converts all characters from the given String to unicodes characters encoded like \uxxxx.
     *
     * @param s
     *            The String to convert.
     * @param toLowerCase
     *            If true the letters from the unicode characters are lower case.
     * @return The converted String.
     */
    public static String convertCharsToUnicodeChars(final String s, final boolean toLowerCase) {
        if (s == null || s.length() == 0) {
            return s;
        }
        int length = s.length();
        int sbLength = length * 2;
        StringBuilder sb = new StringBuilder(sbLength);
        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            if (c > 61 && c < 127) {
                if (c == '\\') {
                    sb.append('\\');
                    sb.append('\\');
                    continue;
                }
                sb.append(c);
                continue;
            }
            switch (c) {
            case '\f':
                sb.append('\\');
                sb.append('f');
                break;
            case '\n':
                sb.append('\\');
                sb.append('n');
                break;
            case '\r':
                sb.append('\\');
                sb.append('r');
                break;
            case '\t':
                sb.append('\\');
                sb.append('t');
                break;
            case ' ':
                if (i == 0) {
                    sb.append('\\');
                }
                sb.append(' ');
                break;
            case ':':
            case '#':
            case '=':
            case '!':
                sb.append('\\');
                sb.append(c);
                break;
            default:
                if (c < 0x0020 || c > 0x007e) {
                    sb.append('\\');
                    sb.append('u');
                    sb.append(toHex(c >> 12 & 0xF));
                    sb.append(toHex(c >> 8 & 0xF));
                    sb.append(toHex(c >> 4 & 0xF));
                    sb.append(toHex(c & 0xF));
                } else {
                    sb.append(c);
                }
            }
        }
        String returnString = sb.toString();
        if (toLowerCase) {
            return returnString.toLowerCase();
        } else {
            return returnString;
        }
    }

    /**
     * To hex.
     *
     * @param i
     *            the i
     * @return the char
     */
    public static char toHex(final int i) {
        return HEXADECIMAL_DIGITS[i & 0xF];
    }
}

Related

  1. convertCharacters(String fileName)
  2. convertCharArray(char[] arr)
  3. convertCharArrayToIntArray(char[] chars)
  4. convertChars(String orig_name, String chars, char toChar)
  5. convertCharSet(String str, String desCharset)
  6. convertCharToBoolean(final char value)
  7. convertCharToCharacterArray(final Object originalArray)
  8. convertCharToInt(char input)
  9. convertCharToShort(final char[] tmp, final short[] buffer)