Search code examples
javadecoder

Convert OCTL character to ISO-8859-15(html) with java


how can i conver a String that contains OCTL character to ISO-8859-15, to make understable. I'm working with java. ty.

i'm trying to recuperate a value for a informix bbdd, an i put inside String, but when i show in view i saw Espa\321a---> and i wanna see ESPAÑA. I've looking for an saw that \321 is a OCTL is this possible ?

HTML OCTL HEX CMP CHR MEANING

Ñ | \321 | =D1 | N ~ | (Ñ) | Capital N, tilde

so OCTL = \321 = Ñ

I try this but don't work for me, i'm doing sth wrong.

Charset charset = Charset.forName("OCTL");
        CharsetDecoder decoder = charset.newDecoder();
        CharsetEncoder encoder = charset.newEncoder();

        try {

            ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(rs.getString(Constants.DES_PAIS_COM)));

            ByteBuffer and then to a string.

            CharBuffer cbuf = decoder.decode(bbuf);
            String s = cbuf.toString();
            deuteDetall.setDesPaisCom(s);

        } catch (CharacterCodingException e) {



        }

Solution

  • \321 is octal, in hex D1, (3*64+2*8+1).

    String s = "...";
    Pattern OCTAL = Pattern.compile("\\\\(\\d\\d\\d)");
    StringBuffer sb = new StringBuffer();
    byte[] b1 = new byte[1];
    Matcher m = OCTAL.matcher(s);
    while (m.find()) {
        String replacement = m.group(): // default original
        try {
            b1[0] = (byte)Integer.parseInt(m.group(1), 8);
            replacement = new String(b1, "ISO-8859-15");
        } catch (NumberFormatException e) {
            //...
        }
        m.appendReplacement(sb, replacement);
    }
    m.appendTail(sb);
    s = sb.toString();