Java Unicode Create toUnicode(final String toUnicode, final boolean toLowerCase)

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

Description

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

License

Apache License

Parameter

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

Return

The converted String.

Declaration

public static String toUnicode(final String toUnicode, final boolean toLowerCase) 

Method Source Code

//package com.java2s;
/**/*from w ww . j  av  a 2 s  . c  o  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 {
    /**
     * Converts all characters from the given String to unicodes characters encoded like \uxxxx.
     *
     * @param toUnicode
     *            The String to convert.
     * @param toLowerCase
     *            If true the letters from the unicode characters are lower case.
     * @return The converted String.
     */
    public static String toUnicode(final String toUnicode, final boolean toLowerCase) {
        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();
    }
}

Related

  1. getUnicodeBytes(String s)
  2. getUnicodeString(byte bytes[], int offset, int len)
  3. getUnicodeString(byte[] d, int length, int pos)
  4. getUnicodeText(Object text)
  5. toUnicode(char c)
  6. toUnicode(String input)
  7. toUnicode(String input)
  8. toUnicode(String input)
  9. toUnicode(String original)