Skip to content

sbai/bagua

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bagua

Bagua is a gem that encodes text as strings of trigrams or hexagrams.

Trigrams and hexagrams are symbols used in Taoist cosmology, e.g. in the I Ching. A trigram consists of three parallel horizontal lines, each of which can be either broken or unbroken. Because each line can be in one of 2 states, and there are 3 lines in total, there are a total of 2^3 = 8 possible combinations, as shown below.

☰ ☱ ☲ ☳ ☴ ☵ ☶ ☷

Each trigram above can be interpreted as a 3-bit binary sequence, where ☰ is 000 and ☷ is 111. Similarly, a hexagram can be interpreted as a 6-bit binary sequence, as it consists of six lines, or two trigrams put together. Consequently, as noted by Leibniz, sequences of trigrams or hexagrams can be used to represent arbitrary binary data.

Because a plain text string is just a representation of an array of numbers, it follows that any string can be encoded as a sequence of trigrams or hexagrams. These trigrams and hexagrams themselves are also just plain text (Unicode) characters.

Usage

require 'bagua'

Encoding

# trigrams
Bagua::Tri.encode("abcde") # => "☳☰☲☶☱☱☴☳☳☱☰☶☲☴"

Historically, hexagrams have been listed in several different sequences. Bagua can encode hexagrams using either the default binary sequence (Fu Xi's sequence) or the King Wen sequence.

# hexagrams in binary sequence
Bagua::Hex.encode("abcde", :binary) # => "䷘䷕䷹䷟䷐䷙䷤"
# same as above but shorter
Bagua::Hex.encode("abcde") # => "䷘䷕䷹䷟䷐䷙䷤"
# King Wen sequence
Bagua::Hex.encode("abcde", :wen) # => "䷘䷖䷉䷣䷙䷆䷔"

Decoding

# trigrams
Bagua::Tri.decode("☳☰☲☶☱☱☴☳☳☱☰☶☲☴") # => "abcde"
# hexagrams in binary sequence
Bagua::Hex.decode("䷘䷕䷹䷟䷐䷙䷤", :binary) # => "abcde"
# same as above but shorter
Bagua::Hex.decode("䷘䷕䷹䷟䷐䷙䷤") # => "abcde"
# King Wen sequence
Bagua::Hex.decode("䷘䷖䷉䷣䷙䷆䷔", :wen) # => "abcde"

About

Trigram/hexagram encoder

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages