Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
EncodingDecoding
#1
This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given. If you are the owner of the thread, please contact administration.


Just a simple idea of mine, the ability to encode/decode a string. Could be used for security in saves (although you need to force decode them) or in puzzles.
First, the addition to the string class:
Code:
class String
  # Just call it as string.encode("list_to_use"), remember, it does not change the
  # String(like sub&gsub) so use string = string.encode("list_to_use") to change
  def encode(codelist = "default")
    if CodeLists.singleton_methods.include?(codelist) and codelist != "empty"
      @codelist = toto("CodeLists.#{codelist}")
    else
      @codelist = CodeLists.default
    end      
    array = self.clone.scan(/./)
    for i in 0..array.size
      next unless @codelist.key?(array[i])
      array[i] = @codelist[array[i]]
    end
    pie = array.join
    pie.codelist = @codelist
    pie
  end
  # A def to set codelist. Protected so it can't be used outside the string.
  def codelist=(codelist)
    unless @codelist.is_a? Hash
      @codelist = codelist
    end
  end
  protected :codelist=
  # Same as encode, but reversed.
  # Use force if you loaded a string from a file or so.
  def decode(force = nil)
    if force != nil
      if CodeLists.singleton_methods.include?(codelist) and codelist != "empty"
        @codelist = toto("CodeLists.#{codelist}")
      else
        @codelist = CodeLists.default
      end
    end
    return self unless @codelist
    array = self.clone.scan(/./)
    for i in 0..array.size
      next unless @codelist.value?(array[i])
      array[i] = @codelist.index(array[i])
    end
    pie = array.join
    pie
  end
end

All instructions are in the comments.
Now, the module containing codelists, with 2 examples, 1 to make random ones and 1 empty one. Note that in these lists symbols and spaces are not programmed but could be done easily (are programmed in random).
Code:
module CodeLists
  # An empty list, for easy builds.
  def self.empty
    list = {}
    list["A"] = ""
    list["B"] = ""
    list["C"] = ""
    list["D"] = ""
    list["E"] = ""
    list["F"] = ""
    list["G"] = ""
    list["H"] = ""
    list["I"] = ""
    list["J"] = ""
    list["K"] = ""
    list["L"] = ""
    list["M"] = ""
    list["N"] = ""
    list["O"] = ""
    list["P"] = ""
    list["Q"] = ""
    list["R"] = ""
    list["S"] = ""
    list["T"] = ""
    list["U"] = ""
    list["V"] = ""
    list["W"] = ""
    list["X"] = ""
    list["Y"] = ""
    list["Z"] = ""
    list["a"] = ""
    list["b"] = ""
    list["c"] = ""
    list["d"] = ""
    list["e"] = ""
    list["f"] = ""
    list["g"] = ""
    list["h"] = ""
    list["i"] = ""
    list["j"] = ""
    list["k"] = ""
    list["l"] = ""
    list["m"] = ""
    list["n"] = ""
    list["o"] = ""
    list["p"] = ""
    list["q"] = ""
    list["r"] = ""
    list["s"] = ""
    list["t"] = ""
    list["u"] = ""
    list["v"] = ""
    list["w"] = ""
    list["x"] = ""
    list["y"] = ""
    list["z"] = ""
    list["0"] = ""
    list["1"] = ""
    list["2"] = ""
    list["3"] = ""
    list["4"] = ""
    list["5"] = ""
    list["6"] = ""
    list["7"] = ""
    list["8"] = ""
    list["9"] = ""
    return list
  end
  # Randomize list. I think this one is the most secure. Note that you will be unable
  # To decode if you save in a file.
  def self.random
    alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P",
    "Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i",
    "j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1",
    "2","3","4","5","6","7","8","9"," ","`","~","!","@","#","$","%","€","^","&",
    "*","(",")","-","_","+","=","[","]","{","}","|","\\",";",":","\"","
An example of a random encodement is attached.
}




Users browsing this thread: