Here you can find the source of convertCharsToUnicodeChars(final String s, final boolean toLowerCase)
Parameter | Description |
---|---|
s | The String to convert. |
toLowerCase | If true the letters from the unicode characters are lower case. |
public static String convertCharsToUnicodeChars(final String s, final boolean toLowerCase)
//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]; } }