fixed the implementation of Bignum#to_ber.

This commit is contained in:
blackhedd 2006-12-15 18:29:26 +00:00
parent 0bf35f8d3e
commit 5f197cc040
3 changed files with 39 additions and 4 deletions

View file

@ -409,9 +409,22 @@ end # class Fixnum
class Bignum
def to_ber
i = [self].pack('w')
i.length > 126 and raise Net::BER::BerError.new( "range error in bignum" )
[2, i.length].pack("CC") + i
#i = [self].pack('w')
#i.length > 126 and raise Net::BER::BerError.new( "range error in bignum" )
#[2, i.length].pack("CC") + i
# Ruby represents Bignums as two's-complement numbers so we may actually be
# good as far as representing negatives goes.
# I'm sure this implementation can be improved performance-wise if necessary.
sz = self.size
out = "\000" * sz
(sz*8).times {|bit|
if self[bit] == 1
out[bit/8] += (1 << (bit % 8))
end
}
[2, sz].pack("CC") + out.reverse
end
end