Is there an existing gem or script that converts numbers to comp-3/packed decimal format?

巧了我就是萌 提交于 2019-12-11 02:46:14

问题


Continuing with my adventure to convert COBOL to a Ruby program, I have to convert a decimal digit to a comp-3/packed decimal format. Anyone know of a simple Ruby script or gem that does this? Berns


回答1:


Ruby knows how to pack nibbles, so it turns out to be quite easy:

def pack_comp(n)
  s = n.abs.to_s + (n < 0 ? "d" : "c")
  s = "0" + s if s.size.odd?
  [s].pack("H*")
end


来源:https://stackoverflow.com/questions/2623997/is-there-an-existing-gem-or-script-that-converts-numbers-to-comp-3-packed-decima

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