Project

bagua

0.0
No commit activity in last 3 years
No release in over 3 years
Encodes plain text as sequences of trigrams or hexagrams.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.16
~> 12.3
~> 3.4
~> 3.7
~> 0.9.12
 Project Readme

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"