Search code examples
javaebcdic

Simple Way in Java to check if char is in EBCDIC


I'm trying to figure out how to check if a char is in EBCDIC.

One approach would be to have a predefined array with all possible EBCDIC signs, and check if the character can be found in that array.

Is there a better/cleaner way?


Solution

  • boolean canEncode(char ch) {
        Charset charset = Charset.forName("EBCDIC");
        return charset.newEncoder().canEncode(ch);
    }
    

    The CharsetEncoder can also check entire Strings.

    And there is more conversion support around Charset.


    Thanks to @BruceMartin and @0x1C1B

    Note that there are more than one EBCDIC variants, just like 8 bit ASCII ((US) ASCII being 7 bits). Pick the right code page: `"Cp037" for US EBCDIC, "Cp1047", ...

    You will need the Charset to convert to EBCDIC bytes.