How do I do ASCII to EBCDIC translation in Ruby?

﹥>﹥吖頭↗ 提交于 2019-12-02 13:38:29

问题


I am using Ruby 1.8.7 on Mac OS X.

How do I convert ASCII to EBCDIC encoding, to communicate with legacy system. Would I have to use to jruby?


回答1:


You can upgrade but that doesn't necessarily solve the problem.

There are multiple flavors of EBCDIC (THANK YOU IBM!) so you'll need to identify the subset your mainframe uses.

One thing I learned to do when programming on the mainframe, oh so many years ago, was to call some of the mainframe sysops, and pick their brains. They deal with conversion from other codesets into EBCDIC all day long, and probably have a tool that can do it on the fly.

An alternative would be to see if they have something that can parse JSON or YAML. Convert your text to UTF-8, send it to the mainframe, let its translator convert from UTF-8 to EBCDIC.




回答2:


You should use the Ruby iconv library (for Ruby versions before 2.0) or the iconv gem (for Ruby 2+) specifying EBCDIC-US as the codeset:

irb(main):001:0> require('iconv')
=> true
irb(main):002:0> x=Iconv.new('EBCDIC-US','ASCII')
=> #<Iconv:0x7fb4274d88d8>
irb(main):003:0> x.iconv("foo")
=> "\206\226\226"


来源:https://stackoverflow.com/questions/4718324/how-do-i-do-ascii-to-ebcdic-translation-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!