Java Unicode Create toUnicodeRepresentation(String str)

Here you can find the source of toUnicodeRepresentation(String str)

Description

to Unicode Representation

License

Apache License

Declaration

public static String toUnicodeRepresentation(String str) 

Method Source Code

//package com.java2s;
/*/*w  ww  .  ja va2 s  .c  o m*/
 * Copyright 2012-2014 Dan Cioca
 *
 * 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 {
    public static String toUnicodeRepresentation(String str) {
        if (str == null)
            return str;
        StringBuilder unicodeString = new StringBuilder();

        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);

            if ((ch >= 0x0020) && (ch <= 0x007e)) {
                // no need to convert to unicode
                unicodeString.append(ch);
            } else {
                // standard unicode format.
                unicodeString.append("\\u");

                // Get hex value of the char.
                String hex = Integer.toHexString(str.charAt(i) & 0xFFFF);

                // Prepend zeros because unicode requires 4 digits
                for (int j = 0; j < 4 - hex.length(); j++)
                    unicodeString.append("0");
                unicodeString.append(hex.toLowerCase()); // standard unicode format.
                //ostr.append(hex.toLowerCase(Locale.ENGLISH));
            }
        }

        return unicodeString.toString();
    }
}

Related

  1. toUnicodeEncoded(String data)
  2. toUnicodeEscape(char c)
  3. toUnicodeEscape(int ch)
  4. toUnicodeLiteral(String s)
  5. toUnicodePoint(char c)
  6. toUnicodeStr(int i)
  7. toUnicodeString(byte[] ba)
  8. toUnicodeString(byte[] data)
  9. toUnicodeString(char c)