module Ciri::Utils::Number
Constants
- UINT_255_CEILING
- UINT_255_MAX
- UINT_256_CEILING
- UINT_256_MAX
Public Instance Methods
big_endian_decode(input)
click to toggle source
# File lib/ciri/utils/number.rb, line 34 def big_endian_decode(input) input.each_byte.reduce(0) {|s, i| s * 256 + i} end
big_endian_encode(n, zero = ''.b, size: nil)
click to toggle source
# File lib/ciri/utils/number.rb, line 29 def big_endian_encode(n, zero = ''.b, size: nil) b = big_endian_encode_raw(n, zero) size.nil? ? b : b.rjust(size, "\x00".b) end
ceil_div(n, ceil)
click to toggle source
# File lib/ciri/utils/number.rb, line 51 def ceil_div(n, ceil) size, m = n.divmod ceil m.zero? ? size : size + 1 end
signed_to_unsigned(n)
click to toggle source
# File lib/ciri/utils/number.rb, line 47 def signed_to_unsigned(n) n >= 0 ? n : n + UINT_256_CEILING end
unsigned_to_signed(n)
click to toggle source
# File lib/ciri/utils/number.rb, line 43 def unsigned_to_signed(n) n <= UINT_255_MAX ? n : n - UINT_256_CEILING end
Private Instance Methods
big_endian_encode_raw(n, zero = ''.b)
click to toggle source
# File lib/ciri/utils/number.rb, line 58 def big_endian_encode_raw(n, zero = ''.b) if n == 0 zero elsif n > 0 big_endian_encode(n / 256) + (n % 256).chr else raise ArgumentError.new("can't encode negative number #{n}") end end