Java Unicode unicodeConvert(String str)

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

Description

unicode Convert

License

Apache License

Declaration

public static String unicodeConvert(String str) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String unicodeConvert(String str) {
        char[] in = str.toCharArray();
        int off = 0;
        int len = str.length();
        char[] out = new char[len];

        int outLen = 0;
        while (off < len) {
            char aChar = in[(off++)];
            if (aChar == '\\') {
                aChar = in[(off++)];/*from  w  ww  .j a  v a 2  s. c om*/
                if (aChar == 'u') {
                    int value = 0;
                    for (int i = 0; i < 4; i++) {
                        aChar = in[(off++)];
                        switch (aChar) {
                        case '0':
                        case '1':
                        case '2':
                        case '3':
                        case '4':
                        case '5':
                        case '6':
                        case '7':
                        case '8':
                        case '9':
                            value = (value << 4) + aChar - 48;
                            break;
                        case 'a':
                        case 'b':
                        case 'c':
                        case 'd':
                        case 'e':
                        case 'f':
                            value = (value << 4) + 10 + aChar - 97;
                            break;
                        case 'A':
                        case 'B':
                        case 'C':
                        case 'D':
                        case 'E':
                        case 'F':
                            value = (value << 4) + 10 + aChar - 65;
                            break;
                        case ':':
                        case ';':
                        case '<':
                        case '=':
                        case '>':
                        case '?':
                        case '@':
                        case 'G':
                        case 'H':
                        case 'I':
                        case 'J':
                        case 'K':
                        case 'L':
                        case 'M':
                        case 'N':
                        case 'O':
                        case 'P':
                        case 'Q':
                        case 'R':
                        case 'S':
                        case 'T':
                        case 'U':
                        case 'V':
                        case 'W':
                        case 'X':
                        case 'Y':
                        case 'Z':
                        case '[':
                        case '\\':
                        case ']':
                        case '^':
                        case '_':
                        case '`':
                        default:
                            throw new IllegalArgumentException("Malformed \\uxxxx encoding.");
                        }
                    }
                    out[(outLen++)] = ((char) value);
                } else {
                    if (aChar == 't') {
                        aChar = '\t';
                    } else if (aChar == 'r') {
                        aChar = '\r';
                    } else if (aChar == 'n') {
                        aChar = '\n';
                    } else if (aChar == 'f') {
                        aChar = '\f';
                    }
                    out[(outLen++)] = aChar;
                }
            } else {
                out[(outLen++)] = aChar;
            }
        }
        return new String(out, 0, outLen);
    }
}

Related

  1. unicode2Byte(String s)
  2. Unicode2GBK(String dataStr)
  3. unicode2native(String s)
  4. unicodeChar(char a, char b, char c, char d)
  5. unicodeCodepointToString(char cp, boolean shortenIfPossible, String prefix, boolean upperCase)
  6. unicodeCount(String sStr)
  7. unicodeEncode(String s)
  8. unicodeHTMLEscape(final String s)
  9. unicodePreservingIndex(String str, int index)