Convert String from ASCII to EBCDIC in Java?

后端 未结 10 1678
北荒
北荒 2020-11-28 10:38

I need to write a \'simple\' util to convert from ASCII to EBCDIC?

The Ascii is coming from Java, Web and going to an AS400. I\'ve had a google around, can\'t seem

相关标签:
10条回答
  • 2020-11-28 11:23
    package javaapplication1;
    
    import java.nio.ByteBuffer;
    import java.nio.CharBuffer;
    
    import java.nio.charset.CharacterCodingException;
    
    import java.nio.charset.Charset;
    
    import java.nio.charset.CharsetDecoder;
    
    import java.nio.charset.CharsetEncoder;
    
    public class ConvertBetweenCharacterSetEncodingsWithCharBuffer {
    
        public static void main(String[] args) {
    
           //String cadena = "@@@@@@@@@@@@@@@ñâæÃÈÄóöó@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ÔÁâãÅÙÃÁÙÄ@ÄÅÂÉã@âæÉãÃÈ@@@@@@@@";
            String cadena = "ñâæÃÈÄóöó";
            System.out.println(Convert(cadena,"CP1047","ISO-8859-1"));
            cadena = "1SWCHD363";
            System.out.println(Convert(cadena,"ISO-8859-1","CP1047"));
    
        }
    
        public static String Convert (String strToConvert,String in, String out){
           try {
    
            Charset charset_in = Charset.forName(out);
            Charset charset_out = Charset.forName(in);
    
            CharsetDecoder decoder = charset_out.newDecoder();
    
            CharsetEncoder encoder = charset_in.newEncoder();
    
            CharBuffer uCharBuffer = CharBuffer.wrap(strToConvert);
    
            ByteBuffer bbuf = encoder.encode(uCharBuffer);
    
            CharBuffer cbuf = decoder.decode(bbuf);
    
            String s = cbuf.toString();
    
            //System.out.println("Original String is: " + s);
            return s;
    
        } catch (CharacterCodingException e) {
    
            //System.out.println("Character Coding Error: " + e.getMessage());
            return "";
    
        }
    
    
    }
    
    }
    
    0 讨论(0)
  • 2020-11-28 11:26

    You should use either the Java character set Cp1047 (Java 5) or Cp500 (JDK 1.3+).

    Use the String constructor: String(byte[] bytes, [int offset, int length,] String enc)

    0 讨论(0)
  • 2020-11-28 11:26

    This is what I've been using.

    public static final int[] ebc2asc = new int[256];
    public static final int[] asc2ebc = new int[256];
    
    static
    {
      byte[] values = new byte[256];
      for (int i = 0; i < 256; i++)
        values[i] = (byte) i;
    
      try
      {
        String s = new String (values, "CP1047");
        char[] chars = s.toCharArray ();
        for (int i = 0; i < 256; i++)
        {
          int val = chars[i];
          ebc2asc[i] = val;
          asc2ebc[val] = i;
        }
      }
      catch (UnsupportedEncodingException e)
      {
        e.printStackTrace ();
      }
    }
    
    0 讨论(0)
  • 2020-11-28 11:31

    I make a code that transforms data types easily.

    public class Converter{
    
        public static void main(String[] args) {
    
            Charset charsetEBCDIC = Charset.forName("CP037");
            Charset charsetACSII = Charset.forName("US-ASCII");
    
            String ebcdic = "(((((((";
            System.out.println("String EBCDIC: " + ebcdic);
            System.out.println("String converted to ASCII: " + convertTO(ebcdic, charsetEBCDIC, charsetACSII));
    
            String ascII = "MMMMMM";
            System.out.println("String ASCII: " + ascII);
            System.out.println("String converted to EBCDIC: " + convertTO(ascII, charsetACSII, charsetEBCDIC));
        }
    
        public static String convertTO(String dados, Charset encondingFrom, Charset encondingTo) {
            return new String(dados.getBytes(encondingFrom), encondingTo);
        }
    }
    
    0 讨论(0)
提交回复
热议问题