Snippet





//------------------------------------------------------------------------------------------------------------------------

public static String unicodeToHexEscapedUTF(String s) {

        HashMap<String, String> toReplace = new HashMap<String, String>();
        for (int i = 0; i < s.length(); i++) {
            if (!isPureAscii(s.charAt(i) + "")) {

                String hexStr = Integer.toHexString((int) s.charAt(i));
                for (int j = 0; j <= 4 - hexStr.length(); j++)
                    hexStr = '0' + hexStr;
                hexStr = "\\\\u" + hexStr;
                System.out.println(s.charAt(i) + " ___ " + hexStr);

                toReplace.put(s.charAt(i) + "", hexStr);
            }
        }

        Iterator it = toReplace.keySet().iterator();
        String unicodeChar, hexEscapedChar;
        while (it.hasNext()) {
            unicodeChar = (String) it.next();
            hexEscapedChar = toReplace.get(unicodeChar);
           
            s = s.replaceAll(unicodeChar, hexEscapedChar);
        }
        return s;
    }

    public static boolean isPureAscii(String v) {
        CharsetEncoder asciiEncoder = Charset.forName("US-ASCII").newEncoder();
        return asciiEncoder.canEncode(v);
    }


@Test
    public void testUnicodeToEscapedUTF() {
        String unicode;
        unicode = "ęaíb ßęĦŽ";

        String expected, actual;

        expected = "\\u0119a\\u00edb \\u00df\\u0119\\u0126\\u017d";
        actual = MergeProperties.unicodeToEscapedUTF(unicode);

        assertEquals(expected, actual);
    }

//------------------------------------------------------------------------------------------------------------------------




























Comments