Hi There, 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
views:
69answers:
1
+3
A:
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
Marc-André Lafortune
2010-04-12 20:20:48
wow...I'm an idiot. Thanks Mark!
btelles
2010-04-12 21:09:02
No problem. And no, you're not. BTW, it did take me more than 5 minutes :-) And my first solution was overkill too.
Marc-André Lafortune
2010-04-12 21:21:04
Idiots are only geniuses who don't realise it yet.
Ryan Bigg
2010-04-12 21:34:47