在 Java 中将字符串从 ASCII 转换为 EBCDIC?
我需要编写一个“简单”实用程序来从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
请注意,Java 中的字符串以 Java 的本机编码保存文本。 当在内存中保存 ASCII 或 EBCDIC“字符串”时,在编码为字符串之前,您将把它放在 byte[] 中。
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[].
JTOpen,IBM 的 Java 工具箱的开源版本有一组用于访问 AS/400 对象的类,其中包括FileReader 和 FileWriter 用于访问本机 AS400 文本文件。 这可能比编写您自己的转换类更容易使用。
来自 JTOpen 主页:
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:
您应该使用 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)
您可以使用此翻译表自行创建一个。
但是这里是一个包含Java示例链接的站点。
You can create one yoursef with this translation table.
But here is a site that has a link to a Java example.
也许,像我一样你并没有严格使用 JDBC功能(在我的实例中写入数据队列),因此 auto-magical 编码不适用于您,因为我们通过多个 API 进行通信。
我的问题与 @scottyab 的问题类似,某些字符未映射。 就我而言,我引用的示例代码工作得很好,但是将 xml 字符串写入数据队列会导致 [ 被 £ 替换。
作为一名使用包含数十年信息的现有数据库后端的 Web 开发人员,我并不像其他评论者所建议的那样简单地具有“纠正”“错误配置”的能力。
但是,通过向 400 发出命令来显示已知良好文件的文件字段信息,我能够看到 i 可能使用哪个编码字符集标识符:
DSPFFD *LIB*/*FILE*
。这样做给了我很好的信息,包括特定的 CCSID 集:
在在 CCSID 上查找信息之后,我遇到了一个页面在 IBM 上的 EBCDIC页面上印有关键信息(因为它有消失的习惯):
最有帮助的是:
因为我已经从这个问题本身了解到
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:
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):
And most helpful:
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 triedCp37
to see no such charsset existed, but attemptedCp037
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.
我编写了一个可以轻松转换数据类型的代码。
I make a code that transforms data types easily.
我想补充一下 Kwebble 和 Shawn S 所说的内容。 我可以使用 JTOpen 来做到这一点。
我需要写入一个 6 0P 的字段(6 个字节,小数点后没有任何内容,已打包)。 对于那些不懂 DDM 的人来说,这是一个小数 (11,0)。
是的,我使用了提到的 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.
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.
为 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.
这就是我一直在使用的。
This is what I've been using.