# Ruby [Ruby Quiz] Base32 字母表 —— 加密猫基因解码

dsh0416 · 2018年12月08日 · 最后由 luobo 回复于 2018年12月09日 · 1010 次阅读

## 题目翻译

Q: 什么是 kai 标注？

Kai 标注（因为 Kai Turner 解码了加密猫的基因而命名）是一种针对 240 位整数分割成 5 位块的 base58 的变种（子集）。每个 5 位块含有 32 种可能性，240 位基因可以给分割成 12 组，每组 4 (x 5 位）基因。

Kai Binary Num Kai Binary Num Kai Binary Num Kai Binary Num
1 00000 00 9 01000 08 h 10000 16 q 11000 24
2 00001 01 a 01001 09 i 10001 17 r 11001 25
3 00010 02 b 01010 10 j 10010 18 s 11010 26
4 00011 03 c 01011 11 k 10011 19 t 11011 27
5 00100 04 d 01100 12 m 10100 20 u 11100 28
6 00101 05 e 01101 13 n 10101 21 v 11101 29
7 00110 06 f 01110 14 o 10110 22 w 11110 30
8 00111 07 g 01111 15 p 10111 23 x 11111 31

``````
# A 240-bit super "sekretoooo" integer genome

genome = 0x4a52931ce4085c14bdce014a0318846a0c808c60294a6314a34a1295b9ce
# decimal (base 10)
genome = 512955438081049600613224346938352058409509756310147795204209859701881294
# binary (base 2)
genome = 0b010010100101001010010011000111001110010000001000010111000001010010111101110011100000000101001010000000110001100010000100\
011010100000110010000000100011000110000000101001010010100110001100010100101000110100101000010010100101011011100111001110
``````

``````p genome    # printed as decimal (base 10) by default
# => 512955438081049600613224346938352058409509756310147795204209859701881294

p genome.to_s(16)
# => "4a52931ce4085c14bdce014a0318846a0c808c60294a6314a34a1295b9ce"

p genome.to_s(2)
# => "10010100101001010010011000111001110010000001000010111000001010010111101110011100000000101001010000000110001100010000100\
#     011010100000110010000000100011000110000000101001010010100110001100010100101000110100101000010010100101011011100111001110"

bin = '%0240b' % genome     # note: adds leading zeros - to_s(2) does not
p bin.size
# => 240
p bin
# => "010010100101001010010011000111001110010000001000010111000001010010111101110011100000000101001010000000110001100010000100\
#     011010100000110010000000100011000110000000101001010010100110001100010100101000110100101000010010100101011011100111001110"

hex = '%060x' % genome     # note: adds leading zeros - to_s(16) does not
p hex.size
# => 60
p hex
# => 60
# => "4a52931ce4085c14bdce014a0318846a0c808c60294a6314a34a1295b9ce"
``````

``````kai = kai_encode( genome )   ## number to base32 kai notation
p kai
# => "aaaa788522f2agff16617755e979244166677664a9aacfff"
``````

``````def kai_encode( num )
# ...
end
``````

``````"aaaa788522f2agff16617755e979244166677664a9aacfff"
``````

``````"aaaa 7885 22f2 agff 1661 7755 e979 2441 6667 7664 a9aa cfff"
``````
``````def kai_fmt( kai )
# ...
end
``````

``````require 'minitest/autorun'

class RubyQuizTest < MiniTest::Test

################################
# test data
def genomes
[
[0x00004a52931ce4085c14bdce014a0318846a0c808c60294a6314a34a1295b9ce,
"aaaa 7885 22f2 agff 1661 7755 e979 2441 6667 7664 a9aa cfff"]
]
end

#############
# tests
def test_kai_encode
genomes.each do |pair|
num       = pair[0]
exp_value = pair[1].gsub(' ','')   # note: remove spaces

assert_equal exp_value, kai_encode( num )
end
end # method test_kai_encode

def test_kai_fmt
genomes.each do |pair|
kai       = pair[1].gsub(' ','') # remove spaces
exp_value = pair[1]

assert_equal exp_value, kai_fmt( kai )
end
end # method test_kai_fmt

end # class RubyQuizTest
``````

## 剧透时间

``````def kai_encode(num)
num.to_s(2).rjust(240, '0').scan(/.{5}/).map {|n| '123456789abcdefghijkmnopqrstuvwx'[n.to_i(2)]}.join
end

def kai_fmt(kai)
kai.scan(/.{4}/).join(' ')
end

kai_encode(0x00004a52931ce4085c14bdce014a0318846a0c808c60294a6314a34a1295b9ce) # => "aaaa788522f2agff16617755e979244166677664a9aacfff"
kai_fmt(kai_encode(genome)) # => "aaaa 7885 22f2 agff 1661 7755 e979 2441 6667 7664 a9aa cfff"
``````

dsh0416 回复

ruby太好用了 好喜欢

dsh0416 回复
``````def kai_encode num
num.to_s(32).tr('0-9a-v', '1-9a-km-x').ljust 48, '1'
end
``````
luikore 回复

dsh0416 回复