Java Comparator for EBCDIC String

I came across a requirement when I need to convert a string to EBCDIC encoding and then sort it. We need to sort it using EBCDIC, because the line must go in the mainframe. The string that I am sorting will only have alphabets in whole and integer numbers.

I searched for it and then came across a link from IBM that indicated the characters in order

I realized that EBCDIC sorting is completely the opposite of normal java lexicographic sorting (at least for the type of data I'm going to process).

My question is my implementation? If not what I am missing? OR is there any Java comparator for encoding EBCDIC.

+4
source share
3 answers

Since the char type is implicitly UTF-16 in Java EBCDIC, strings need to be compared as Java byte arrays.

Example:

    Charset encoding = Charset.forName("IBM1047");
    Comparator<String> encComparator = (s1, s2) ->
            encoding.encode(s1)
                    .compareTo(encoding.encode(s2));
+5
source

You do not have to spend a lot of time figuring out many of the features of EBCDIC. Given the limited scope of your problem, a simple approach to fulfilling your requirements is as follows:

  • Implement a helper method that reads EBCDIC and creates java.lang.StringJava source code (UTF-16)
  • Implement a helper method that accepts java.lang.StringJava source encoding (UTF-16) and creates an EBCDIC encoded string
  • Use the first method to read data. Sort and do another treatment as needed. Use the second method to write data to the mainframe.

, EBCDIC - , , , . Java , , , , EBCDIC .

+5

Yes, there is a comparator for encoding EBCDIC. Here is the code for it.

`Comparator<Entity Class name> EBCDIC = new Comparator<Entity Class name>() 

     {  
        Charset encoding = Charset.forName("cp500");

   @Override         
  public int compare(Entity Class name jc1, 
       Entity Class name jc2) {             
          return (int) (encoding.encode(jc1.toString()).compareTo(encoding.encode(jc2.toString())));         
        }     
      };   
+1
source

Source: https://habr.com/ru/post/1546794/


All Articles