是否有现有的gem或脚本将数字转换为comp-3 /压缩十进制格式? [英] Is there an existing gem or script that converts numbers to comp-3/packed decimal format?

查看:115
本文介绍了是否有现有的gem或脚本将数字转换为comp-3 /压缩十进制格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续我的冒险,将COBOL转换为Ruby程序,我必须将十进制数字转换为comp-3 /压缩十进制格式。任何人都知道这样做的简单Ruby脚本或gem吗?
Berns

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知道如何打包半字节,所以事实证明这很容易:

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

这篇关于是否有现有的gem或脚本将数字转换为comp-3 /压缩十进制格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆