Java Unicode Escape unicodeEscaped(char ch)

Here you can find the source of unicodeEscaped(char ch)

Description

unicode Escaped

License

Apache License

Declaration

public static String unicodeEscaped(char ch) 

Method Source Code

//package com.java2s;
/**//from ww  w . j  ava  2  s  . c o m
 * Copyright (C) 2017 Lucifer Wong
 *
 * 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 unicodeEscaped(char ch) {
        if (ch < 0x10) {
            return "\\u000" + Integer.toHexString(ch);
        } else if (ch < 0x100) {
            return "\\u00" + Integer.toHexString(ch);
        } else if (ch < 0x1000) {
            return "\\u0" + Integer.toHexString(ch);
        }
        return "\\u" + Integer.toHexString(ch);
    }

    public static String toHexString(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();

        for (int i = 0; i < bytes.length; i++) {
            hexString.append(enoughZero(Integer.toHexString(bytes[i] & 0xff), 2));
        }
        return hexString.toString();
    }

    public static String enoughZero(String str, int len) {
        while (str.length() < len) {
            str = "0" + str;
        }
        return str;
    }

    public static String toString(Object object, String nullStr) {
        return object == null ? nullStr : object.toString();
    }
}

Related

  1. unicodeEscape(char c)
  2. unicodeEscape(Character ch)
  3. unicodeEscape(String s)
  4. unicodeEscape(String s)
  5. unicodeEscape(StringBuilder result, int in, int index)
  6. unicodeEscaped(Character ch)
  7. unicodeEscaped(Character ch)
  8. unicodeEscaped(final Character ch)
  9. unicodeEscaped(final String str)