문제

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

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top