在 Java 中将字符串从 ASCII 转换为 EBCDIC?

发布于 2024-07-10 13:01:00 字数 306 浏览 9 评论 0原文

我需要编写一个“简单”实用程序来从 ASCII 转换为 EBCDIC?

Ascii 是从 Java、Web 发展到 AS400。 我用谷歌搜索了一下,似乎找不到一个简单的解决方案(也许因为没有一个:()。我希望有一个开源实用程序或为已经编写的实用程序付费。

也许像这样?

Converter.convertToAscii(String textFromAS400)
Converter.convertToEBCDIC(String textFromJava)

谢谢,

斯科特

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 to find a easy solution (maybe coz there isn't one :( ). I was hoping for an opensource util or paid for util that has already been written.

Like this maybe?

Converter.convertToAscii(String textFromAS400)
Converter.convertToEBCDIC(String textFromJava)

Thanks,

Scott

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

樱娆 2024-07-17 13:01:00

请注意,Java 中的字符串以 Java 的本机编码保存文本。 当在内存中保存 ASCII 或 EBCDIC“字符串”时,在编码为字符串之前,您将把它放在 byte[] 中。

ASCII -> Java:   new String(bytes, "ASCII")
EBCDIC -> Java:  new String(bytes, "Cp1047")
Java -> ASCII:   string.getBytes("ASCII")
Java -> EBCDIC:  string.getBytes("Cp1047")

Please note that a String in Java holds text in Java's native encoding. When holding an ASCII or EBCDIC "string" in memory, prior to encoding as a String, you'll have it in a byte[].

ASCII -> Java:   new String(bytes, "ASCII")
EBCDIC -> Java:  new String(bytes, "Cp1047")
Java -> ASCII:   string.getBytes("ASCII")
Java -> EBCDIC:  string.getBytes("Cp1047")
大姐,你呐 2024-07-17 13:01:00

JTOpen,IBM 的 Java 工具箱的开源版本有一组用于访问 AS/400 对象的类,其中包括FileReader 和 FileWriter 用于访问本机 AS400 文本文件。 这可能比编写您自己的转换类更容易使用。

来自 JTOpen 主页:

以下只是您可以使用 JTOpen 访问的众多 i5/OS 和 OS/400 资源中的一小部分:

  • 数据库 - JDBC (SQL) 和记录级访问 (DDM)
  • 集成文件系统
  • 程序调用
  • 命令
  • 数据队列
  • 数据区域
  • 打印/假脱机资源
  • 产品和 PTF 信息
  • 作业和作业日志
  • 消息、消息队列、消息文件
  • 用户和群组
  • 用户空间
  • 系统值
  • 系统状态

JTOpen, IBM's open source version of their Java toolbox has a collection of classes to access AS/400 objects, including a FileReader and FileWriter to access native AS400 text files. That may be easier to use then writing your own conversion classes.

From the JTOpen homepage:

Here are just a few of the many i5/OS and OS/400 resources you can access using JTOpen:

  • Database -- JDBC (SQL) and record-level access (DDM)
  • Integrated File System
  • Program calls
  • Commands
  • Data queues
  • Data areas
  • Print/spool resources
  • Product and PTF information
  • Jobs and job logs
  • Messages, message queues, message files
  • Users and groups
  • User spaces
  • System values
  • System status
ヤ经典坏疍 2024-07-17 13:01:00

您应该使用 Java 字符集 Cp1047 (Java 5) 或 Cp500 (JDK 1.3+)。

使用字符串构造函数:String(byte[] bytes, [int offset, int length,] String enc)

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)

有木有妳兜一样 2024-07-17 13:01:00
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 "";

    }


}

}
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 "";

    }


}

}
相思故 2024-07-17 13:01:00

您可以使用此翻译表自行创建一个。

但是这里是一个包含Java示例链接的站点。

You can create one yoursef with this translation table.

But here is a site that has a link to a Java example.

悟红尘 2024-07-17 13:01:00

也许,像我一样你并没有严格使用 JDBC功能(在我的实例中写入数据队列),因此 auto-magical 编码不适用于您,因为我们通过多个 API 进行通信。

我的问题与 @scottyab 的问题类似,某些字符未映射。 就我而言,我引用的示例代码工作得很好,但是将 xml 字符串写入数据队列会导致 [ 被 £ 替换。

作为一名使用包含数十年信息的现有数据库后端的 Web 开发人员,我并不像其他评论者所建议的那样简单地具有“纠正”“错误配置”的能力

但是,通过向 400 发出命令来显示已知良好文件的文件字段信息,我能够看到 i 可能使用哪个编码字符集标识符:DSPFFD *LIB*/*FILE*

这样做给了我很好的信息,包括特定的 CCSID 集:
CCSID 标识符

在 CCSID 上查找信息之后,我遇到了一个页面在 IBM 上的 EBCDIC页面上印有关键信息(因为它有消失的习惯):

版本 11.0.0 扩展二进制编码十进制交换码 (EBCDIC)
是一种通常在 zSeries (z/OS®) 上使用的编码方案,并且
iSeries (System i®)。

最有帮助的是:

一些 EBCDIC CCSID 示例为 37、500 和 1047。

因为我已经从这个问题本身了解到 Cp1047< /code> 是另一个值得尝试的好字符集(这一次,£ 变成了带重音的“Y”),我尝试了 Cp37 发现不存在这样的字符集,但尝试了 Cp037 并获得了正确的编码。

看起来关键是找到您的系统中使用的编码字符集标识符(CCSID),并确保您的 jt400 实例 -否则正在完善 - 与 as400 上的编码集 100% 匹配,就我而言,早于我的一生和几十年前的业务逻辑。

Perhaps, like me you were not strictly using a JDBC feature (Writing to a Dataqueue, in my instance), so the auto-magical encoding didn't apply to you since we're communicating through multiple APIs.

My issue was similar to @scottyab's issue with certain characters not mapping. In my case, the example code I was referencing worked perfectly, but writing an xml string to a dataqueue resulted in [ being replaced with £.

As a web developer working with a pre-existing database backend with decades of information, I didn't simply have the ability to "right" the "mis-configuration" as one other commenter suggests.

However, I was able to see which Coded Character Set Identifier the i was likely using by issuing a command to the 400 to display file field information on a known good file: DSPFFD *LIB*/*FILE*.

Doing so gave me good information, including the specific CCSID set:
CCSID Identifier

After some information sought on CCSIDs, I ran into a page on IBM for EBCDIC with key information printed on the page (since that has a habit of disappearing):

Version 11.0.0 Extended Binary Coded Decimal Interchange Code (EBCDIC)
is an encoding scheme that is typically used on zSeries (z/OS®) and
iSeries (System i®).

And most helpful:

Some example EBCDIC CCSIDs are 37, 500, and 1047.

Since I already learned from this question itself that Cp1047 is another good character set to try (This time, the £ turned into an accented "Y"), I tried Cp37 to see no such charsset existed, but attempted Cp037 and got the right encoding.

It looks like the key is finding which Coded Character Set Identifier (CCSID) is used in your system, and ensuring that your jt400 instance - which otherwise is working perfecting - matches up 100% to the encoding set on the as400, in my case way before my lifetime and decades of business logic ago.

掩于岁月 2024-07-17 13:01:00

我编写了一个可以轻松转换数据类型的代码。

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);
    }
}

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);
    }
}
旧话新听 2024-07-17 13:01:00

我想补充一下 Kwebble 和 Shawn S 所说的内容。 我可以使用 JTOpen 来做到这一点。

我需要写入一个 6 0P 的字段(6 个字节,小数点后没有任何内容,已打包)。 对于那些不懂 DDM 的人来说,这是一个小数 (11,0)。

    AS400PackedDecimal convertedCustId = new AS400PackedDecimal(11, 0);
    byte[] packedCust = convertedCustId.toBytes((int) custId);

    String packedCustStr = new String(packedCust, "Cp037");

    StringBuilder jcommData = new StringBuilder();
    jcommData.append(String.format("%6s", packedCustStr));

是的,我使用了提到的 KWebble 库。 正如 Shawn S 提到的,查看 DSPPFD,我发现该表使用的是 CCSID 37。这有效。

我最初按照 Alan Krueger 的建议尝试使用 Cp1047。 这似乎有效。 不幸的是,如果我的 custId 以 5 结尾,则呈现到文件中的数据是 B0 而不是 5F。 将其更改为 Cp037 修复了该问题。

I want to add on to what Kwebble and Shawn S have said. I can use JTOpen to do this.

I needed to write to a field which was 6 0P (6 bytes, nothing behind the decimal, packed). That's a decimal(11,0) for those of you who don't grok DDM.

    AS400PackedDecimal convertedCustId = new AS400PackedDecimal(11, 0);
    byte[] packedCust = convertedCustId.toBytes((int) custId);

    String packedCustStr = new String(packedCust, "Cp037");

    StringBuilder jcommData = new StringBuilder();
    jcommData.append(String.format("%6s", packedCustStr));

Yes, I used the library KWebble mentioned. Looking at DSPPFD as Shawn S mentioned, I discovered that the table was using CCSID 37. This worked.

I originally tried using Cp1047, as per Alan Krueger's suggestion. It seemed to work. Unfortunately, if my custId ended with a 5, the data rendered into the file was B0 instead of 5F. Changing it to Cp037 fixed that.

帥小哥 2024-07-17 13:01:00

为 EBCDIC 字符集编写一个映射,为 ASCII 字符集编写一个映射,并且在每个映射中返回另一个的字符表示形式应该相当简单。 然后只需循环遍历要翻译的字符串,查找映射中的每个字符并将其附加到输出字符串中。

我不知道是否有公开可用的转换器,但编写一个转换器应该不会超过一个小时左右。

It should be fairly simple to write a map for the EBCDIC character set, and one for the ASCII character set, and in each return the character representation of the other. Then just loop over the string to translate, and look up each character in the map and append it to an output string.

I don't know if there are any converter's publicly available, but it shouldn't take more than an hour or so to write one.

仲春光 2024-07-17 13:01:00

这就是我一直在使用的。

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 ();
  }
}

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 ();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文