Class: Bignum
- Inherits:
-
Object
- Object
- Bignum
- Defined in:
- lib/net/ber.rb
Instance Method Summary collapse
Instance Method Details
#to_ber ⇒ Object
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/net/ber.rb', line 464 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 # 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. # Ruby's Bignum#size returns the number of bytes in the internal representation # of the number, but it can and will include leading zero bytes on at least # some implementations. Evidently Ruby stores these as sets of quadbytes. # It's not illegal in BER to encode all of the leading zeroes but let's strip # them out anyway. # out = [0] * size (size * 8).times do |bit| next unless self[bit] == 1 out[bit/8] += (1 << (bit % 8)) end out = out.pack('C*') out.slice!(-1,1) while out.length > 1 and Net::BER.ord(out[-1]).zero? [2, out.length].pack("CC") + out.reverse end |