本文介绍了用于EBCDIC编码的String的Java比较器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到过一个要求,我需要将字符串转换为EBCDIC编码然后对其进行排序。我们需要用EBCDIC对它进行排序,因为字符串必须进入大型机。我将排序的字符串仅包含captial和integers中的字母。

I have come across a requirement where I need to convert a string to EBCDIC encoding and then sort it. We need to sort it with EBCDIC because the string has to go in mainframe. The string I will sort will have only alphabets in captial and integers only.

我搜索了一些然后我遇到了
已按顺序列出了字符

I googled it some and then I came across the link from IBM which has listed the characters in order

我意识到EBCDIC排序与普通的java词典排序完全相反(至少对于我要处理的数据类型)。

What I realized was that EBCDIC sorting is exactly opposite to normal java lexicographic sorting (at least for the type of data which I am going to process).

我的问题是我的认识对吗?如果不是我错过了什么? OR是否有任何可用于EBCDIC编码的java比较器。

My question is my realization right ? If not what I am missing ? OR is there any java comparator available for EBCDIC encoding.

推荐答案

由于char类型在Java中是隐含的UTF-16 EBCDIC字符串需要作为Java字节数组进行比较。

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

示例:

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

这篇关于用于EBCDIC编码的String的Java比较器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 10:06