class Numeric
Public Instance Methods
clear_bits(c)
click to toggle source
# File lib/rbkb/extends/numeric.rb, line 19 def clear_bits(c) (self ^ (self & c)) end
pack(arg)
click to toggle source
shortcut for packing a single number… wtf…
# File lib/rbkb/extends/numeric.rb, line 15 def pack(arg) [self].pack(arg) end
pad(a)
click to toggle source
calculate padding based on alignment(a)
# File lib/rbkb/extends/numeric.rb, line 6 def pad(a) raise "bad alignment #{a.inspect}" unless a.kind_of? Numeric and a > 0 return self < 1 ? a + self : (a-1) - (self-1) % a end
printable?()
click to toggle source
tells you whether a number is within printable range
# File lib/rbkb/extends/numeric.rb, line 12 def printable?; self >= 0x20 and self <= 0x7e; end
to_bytes(order=nil, siz=nil)
click to toggle source
“packs” a number into bytes using bit-twiddling instead of pack()
Uses to_chars
under the hood. See also: to_hex
args:
siz: pack to this size. larger numbers will wrap order: byte order - :big or :little (only :big has meaning)
# File lib/rbkb/extends/numeric.rb, line 53 def to_bytes(order=nil, siz=nil) to_chars(order,siz) {|c| c.chr }.join end
to_chars(order=nil, siz=nil) { |c| ... }
click to toggle source
Returns an array of chars per 8-bit break-up. Accepts a block for some transformation on each byte. (used by to_bytes
and to_hex
under the hood)
args:
order: byte order - :big or :little (only :big has meaning) siz: pack to this size. larger numbers will wrap
# File lib/rbkb/extends/numeric.rb, line 31 def to_chars(order=nil, siz=nil) order ||= Rbkb::DEFAULT_BYTE_ORDER n=self siz ||= self.size ret=[] siz.times do c = (n % 256) if block_given? then (c = yield(c)) end ret << c n=(n >> 8) end return ((order == :big)? ret.reverse : ret) end
to_hex(o=nil, s=nil)
click to toggle source
Converts a number to hex string with width and endian options. “packs” a number into bytes using bit-twiddling instead of pack()
Uses to_chars
under the hood. See also: to_bytes
args:
siz: pack to this size. larger numbers will wrap order: byte order - :big or :little (only :big has meaning)
# File lib/rbkb/extends/numeric.rb, line 67 def to_hex(o=nil, s=nil) to_chars(o,s) {|c| Rbkb::HEXCHARS[c.clear_bits(0xf) >> 4]+Rbkb::HEXCHARS[c.clear_bits(0xf0)] }.join end