piątek, stycznia 09, 2015

Unescape Unicode (for Tibco dealing with international data)

ByteArrayOutputStream repacked = new ByteArrayOutputStream();
byte[] buff = (in+"\0\0\0\0\0\0").getBytes(); //padding to make McCabe happy
for (int i=0; i < buff.length-6; ) {
    if (buff[i] == '\\' && buff[i+1] == 'u') {
        String t = new String(new int[] { Integer.decode("0x"+new String(buff, i+2, 4)) }, 0, 1);
        for (int k=0; k < t.length(); k++) {
            int cp = t.codePointAt(k);
            if (!Character.isIdentifierIgnorable(cp))
                repacked.write(t.getBytes("utf-8"));
        }
        i+=6;
    }
    else {
        repacked.write(buff, i, 1);
        i++;
    }
}
in = repacked.toString(encoding);

0 komentarze: