【问题标题】:Unable to convert Lowercase Letters from EBCDIC to ASCII无法将小写字母从 EBCDIC 转换为 ASCII
【发布时间】:2021-11-29 07:25:49
【问题描述】:

我已经编写了将数据从 EBCDIC 转换为 ASCII 的代码,该代码适用于数字和大写字母,例如 1、2、A、B、C。但是,它不能转换小写字母 l 以及何时打印它们在日志中它打印不可读的字符。以下是我们用于转换的代码片段。

 public String ebcdicToAscii(String ebcdicValue) {
    String asciiBuffer = null;
    logger.debug("converting field value frm ebcdic to Ascii. ");
    try {

      byte[] ebcdicByteArr = ebcdicValue.toString().getBytes(StandardCharsets.ISO_8859_1);
      int textLen = ebcdicByteArr.length;
      AS400Text textConverter = new AS400Text(textLen);
      asciiBuffer = ((String) textConverter.toObject(ebcdicByteArr)).substring(0, textLen);
      System.out.println("value: "+asciiBuffer);
    } catch (Exception e) {
      logger.error("error while converting field value from ebcdic To Ascii:  " + e);
    }
    return asciiBuffer;
  }

如何解决这个问题?

【问题讨论】:

  • 问题出在AS400Text代码上。
  • 您没有将 EBCDIC 转换为 ASCII。假设ebcdicValue123,那么ASCII 字节是{0x31,0x32,0x33},EBCDIC 字节是{0xF1,0xF2,0xF3}。要将{0xF1,0xF2,0xF3} 转换为{0x31,0x32,0x33} 吗?似乎AS400Text 将EBCDIC 字节数组(如{0xF1,0xF2,0xF3})作为输入,并返回123 字符串。
  • 我正在尝试将 {0xF1,0xF2,0xF3} 转换为 {0x31,0x32,0x33}。是的,AS400Text 将 EBCDIC 字节数组作为输入并返回字符串。 AS400Text 有什么问题,我们该如何解决?
  • > AS400Text 有什么问题,我们该如何解决?没有人可以知道,因为没有人知道代码...您已经知道代码可以与数字和大写字母一起使用,因此调用代码看起来不错。您还知道它不适用于小写字母,因此检查的地方可能是转换函数。嗯,但也很可疑:您对无效的 ISO_8859_1 数据使用带有 ISO_8859_1 编码的 getBytes() - 如果您的 Java 运行时支持匹配的 EBCDIC 编码,请使用它。
  • 当您将文件读入字符串时,已经进行了字符集转换,如果不使用正确的字符集,该转换会导致数据丢失。另一方面,当您在将文件读入字符串时指定正确的字符集时,例如new FileReader(file, Charset.forName("IBM500")),你的工作已经完成了。尝试将字符串转换为字符串是没有意义的。

标签: java ascii ebcdic


【解决方案1】:

如果问题只是小写字母,则将小写字母转换为大写:

String a = "{0xf1,0xf2,0xf3}";
    a = a.substring(1, a.length()-1); //delete first and last character {}
    String[] array = a.split(","); //separate with comma
    ArrayList<String> scripts = new ArrayList<String>();
    for (int i=0; i<array.length; i++)
    {
          String b = array[i].substring(0,1).toUpperCase() + array[i].substring(1,2).toLowerCase() + array[i].substring(2).toUpperCase();
        scripts.add(b);
    }
System.out.println(scripts.toString());

输出是:

0xF1,0xF2,0xF3

【讨论】:

  • 我们如何将 EBCDIC 数据转换为大写?
  • 首先您必须将 EBCDIC 文本添加到数组中。然后,如果它们用逗号分隔,则使用 subString 来分隔每个 EBCDIC。然后使用for循环将小写转换为大写并将其保存在数组中。然后简单地使用数组或将其转换为字符串。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-04
  • 2019-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多