Module: Bagua
- Defined in:
- lib/bagua.rb,
lib/bagua/hex.rb,
lib/bagua/tri.rb,
lib/bagua/version.rb
Overview
Provides encoding and decoding functions for trigrams and hexagrams.
Defined Under Namespace
Constant Summary collapse
- VERSION =
Version number of this module.
"0.1.1"
Class Method Summary collapse
-
.bytes_to_ntets(bytes, n) ⇒ Object
Converts an array of bytes (integers representable in 8 bits) to an array of ntets (integers representable in n bits).
-
.ntets_to_bytes(ntets, n) ⇒ Object
Converts an array of ntets (integers representable in n bits) to an array of bytes (integers representable in 8 bits).
Class Method Details
.bytes_to_ntets(bytes, n) ⇒ Object
Converts an array of bytes (integers representable in 8 bits) to an array of ntets (integers representable in n bits).
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/bagua.rb', line 10 def self.bytes_to_ntets(bytes, n) countn = 0 current = 0 ntets = [] bytes.each do |byte| 7.downto(0) do |b| bit = (byte >> b) & 1 current = (current << 1) | bit countn = (countn + 1) % n if (countn == 0) ntets.push(current) current = 0 end end end # handle leftover bits when n doesn't divide evenly into bytes.length*8 if (countn > 0) current = current << (n - countn) ntets.push(current) end return ntets end |
.ntets_to_bytes(ntets, n) ⇒ Object
Converts an array of ntets (integers representable in n bits) to an array of bytes (integers representable in 8 bits).
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/bagua.rb', line 37 def self.ntets_to_bytes(ntets, n) count8 = 0 current = 0 bytes = [] ntets.each do |ntet| (n - 1).downto(0) do |b| bit = (ntet >> b) & 1 current = (current << 1) | bit count8 = (count8 + 1) % 8 if (count8 == 0) bytes.push(current) current = 0 end end end # handle leftover bits when 8 doesn't divide evenly into ntets.length*n if (count8 > 0) current = current << (8 - count8) bytes.push(current) end return bytes end |