开发者

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

Continuing with my adventure t开发者_高级运维o 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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜