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