num = "0000001000000000011000000000000010010011000011110000000000000000"
for n in 0...num.length
temp = num[n]
dec = dec + temp*(2**(num.length - n - 1))
end
puts dec
当我在irb中运行此代码时,以下错误消息是输出。当我在python中编译相同的逻辑时,它工作得很好。我用Google搜索“的RangeError:BIGNUM太大而转换成'长':但是没有找到相关的答案 请帮我:(在此先感谢RangeError:bignum太大,无法转换为'long'
RangeError: bignum too big to convert intolong' from (irb):4:in
*' from (irb):4:inblock in irb_binding' from (irb):2:in
each' from (irb):2 from C:/Ruby193/bin/irb:12:in `'
正如我下面所说的,Ruby有'num.to_i(2)'形式的这种内置形式:-) – 2012-04-05 07:55:30