Re: Ruby Quiz - Challenge #8 - Base32 Alphabet - Convert the Super "Sekretoooo" 240-Bit CryptoKitties Genome to Kai Notation - Annipurrsary!
Gerald Bauer <[email protected]>
| Newsgroups | gmane.comp.lang.ruby.general |
|---|---|
| Message-ID | <CAAxEZd8J7o95HoQssiA49UU_X+0LKsjU8Cuq55eixLXymz__Vg@mail.gmail.com> |
Hello,
Welcome Delton Ding to Ruby Quiz! Great solution. Keep it up.
For reference here's my humble little code snippet / answer -
cut-n-paste from the world's 1st "standard" base32 / kai library / gem
[1] :)):
module Kai
# See https://en.wikipedia.org/wiki/Base58
## Note: alphabet used for encoding
ALPHABET = '123456789abcdefghijkmnopqrstuvwx'
BASE = ALPHABET.length ## 32 chars/letters/digits
# Converts a base10 integer to a base32 string.
def self.encode( num )
buf = String.new
while num >= BASE
mod = num % BASE
buf = ALPHABET[mod] + buf
num = (num - mod)/BASE
end
ALPHABET[num] + buf
end
def self.fmt( kai )
## note: allow spaces; remove them all first
kai = kai.gsub( ' ', '' )
## format in groups of four (4) separated by space
## e.g. ccac7787fa7fafaa16467755f9ee444467667366cccceede
## : ccac 7787 fa7f afaa 1646 7755 f9ee 4444 6766 7366 cccc eede
kai.reverse.gsub( /(.{4})/, '\1 ').reverse.strip
end
end
Cheers. Prost.
[1] https://github.com/cryptocopycats/base32.kai.rb
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>