Java Char Convert To charToEscape(char ch)

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

Description

Return the Java Unicode escape sequence for the given character.

License

Apache License

Parameter

Parameter Description
ch Character to be converted.

Return

String containing the Java Unicode escape sequence for the given character.

Declaration

public static String charToEscape(char ch) 

Method Source Code

//package com.java2s;
/*/*  ww w.  jav  a  2  s.c o  m*/
 * Copyright (C) 2014 Dell, Inc.
 * 
 * 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 {
    /**
     * Return the Java Unicode escape sequence for the given character. For example, the
     * null character (0x00) is converted to the string "\u0000". This method is useful
     * for creating display-friendly strings that contain hidden non-printable characters.
     *
     * @param ch    Character to be converted.
     * @return      String containing the Java Unicode escape sequence for the given
     *              character.
     */
    public static String charToEscape(char ch) {
        String hexValue = Integer.toHexString(ch);
        if (hexValue.length() == 1) {
            return "\\u000" + hexValue;
        }
        if (hexValue.length() == 2) {
            return "\\u00" + hexValue;
        }
        if (hexValue.length() == 3) {
            return "\\u0" + hexValue;
        }
        return "\\u" + hexValue;
    }
}

Related

  1. charToCompressedBaseLow(final int base)
  2. charToCompressedBaseLow(final int base)
  3. charToCp1252(char c)
  4. charToDecimal(char c)
  5. charToDigit(char c)
  6. charToHTML(char ch, boolean xml)
  7. charToLong(final char value)
  8. charToLowerCase(char c)
  9. charToNCRef(int c)