Class: Numeric
Instance Method Summary collapse
- #clear_bits(c) ⇒ Object
-
#pack(arg) ⇒ Object
shortcut for packing a single number…
-
#pad(a) ⇒ Object
calculate padding based on alignment(a).
-
#printable? ⇒ Boolean
tells you whether a number is within printable range.
-
#randomize ⇒ Object
just to go with the flow.
-
#to_bytes(order = nil, siz = nil) ⇒ Object
“packs” a number into bytes using bit-twiddling instead of pack().
-
#to_chars(order = nil, siz = nil) ⇒ Object
Returns an array of chars per 8-bit break-up.
-
#to_hex(o = nil, s = nil) ⇒ Object
Converts a number to hex string with width and endian options.
Instance Method Details
#clear_bits(c) ⇒ Object
607 |
# File 'lib/rbkb/extends.rb', line 607 def clear_bits(c) ; (self ^ (self & c)) ; end |
#pack(arg) ⇒ Object
shortcut for packing a single number… wtf…
605 |
# File 'lib/rbkb/extends.rb', line 605 def pack(arg) ; [self].pack(arg) ; end |
#pad(a) ⇒ Object
calculate padding based on alignment(a)
593 594 595 596 |
# File 'lib/rbkb/extends.rb', line 593 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? ⇒ Boolean
tells you whether a number is within printable range
599 |
# File 'lib/rbkb/extends.rb', line 599 def printable?; self >= 0x20 and self <= 0x7e; end |
#randomize ⇒ Object
just to go with the flow
602 |
# File 'lib/rbkb/extends.rb', line 602 def randomize ; rand(self) ; end |
#to_bytes(order = nil, siz = nil) ⇒ Object
“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)
639 640 641 |
# File 'lib/rbkb/extends.rb', line 639 def to_bytes(order=nil, siz=nil) to_chars(order,siz) {|c| c.chr }.join end |
#to_chars(order = nil, siz = nil) ⇒ Object
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
617 618 619 620 621 622 623 624 625 626 627 628 629 |
# File 'lib/rbkb/extends.rb', line 617 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) ⇒ Object
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)
653 654 655 656 657 |
# File 'lib/rbkb/extends.rb', line 653 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 |