Class: BigInteger

Inherits:
BinData::BasePrimitive
  • Object
show all
Defined in:
lib/big_integer.rb

Instance Method Summary collapse

Instance Method Details

#read_and_return_value(io) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/big_integer.rb', line 19

def read_and_return_value(io)
  value = 0
  bit_shift = 0

  loop do
    byte = read_uint8(io)
    has_more = byte & 0x80
    seven_bit_byte = byte & 0x7f
    value |= seven_bit_byte << bit_shift
    bit_shift += 7

    break if has_more.zero?
  end

  value
end

#read_uint8(io) ⇒ Object



40
41
42
# File 'lib/big_integer.rb', line 40

def read_uint8(io)
  io.readbytes(1).unpack("C").at(0)
end

#sensible_defaultObject



36
37
38
# File 'lib/big_integer.rb', line 36

def sensible_default
  0
end

#value_to_binary_string(value) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/big_integer.rb', line 2

def value_to_binary_string(value)
  value = value.abs
  bytes = []

  loop do
    seven_bit_byte = value & 0x7f
    value >>= 7
    has_more = value.nonzero? ? 0x80 : 0
    byte = has_more | seven_bit_byte
    bytes.push(byte)

    break if has_more.zero?
  end

  bytes.collect { |b| b.chr }.join
end