如何在ruby中一次解包7位?

时间:2015-11-24 21:09:53

标签: ruby binary uuid base58

我尝试将UUIDv4格式化为url友好字符串。 base16中的典型格式很长,并且有破折号:

xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx

为了避免破折号和下划线,我将使用base58(就像比特币一样),因此每个字符都完全编码sqrt(58).floor = 7 bits

我可以将uuid打包成二进制文件:

[ uuid.delete('-') ].pack('H*')

要获得8位无符号整数:

binary.unpack('C*')

如何将每个7位解包为8位无符号整数?是否有一种模式一次扫描7位并将高位设置为0?

2 个答案:

答案 0 :(得分:1)

require 'base58'
uuid ="123e4567-e89b-12d3-a456-426655440000"
Base58.encode(uuid.delete('-').to_i(16))
=> "3fEgj34VWmVufdDD1fE1Su"

再回来

Base58.decode("3fEgj34VWmVufdDD1fE1Su").to_s(16)
 => "123e4567e89b12d3a456426655440000"

从模板重建uuid格式的便捷模式

template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
src = "123e4567e89b12d3a456426655440000".each_char
template.each_char.reduce(''){|acc, e| acc += e=='-' ? e : src.next}  
 => "123e4567-e89b-12d3-a456-426655440000"      

答案 1 :(得分:1)

John La Rooy的答案很棒,但我只是想指出Base58算法有多简单,因为我认为它很简洁。 (松散地基于base58 gem,加上原始int_to_uuid奖励功能):

ALPHABET = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ".chars
BASE = ALPHABET.size

def base58_to_int(base58_val)
  base58_val.chars
    .reverse_each.with_index
    .reduce(0) do |int_val, (char, index)|
      int_val + ALPHABET.index(char) * BASE ** index
    end
end

def int_to_base58(int_val)
  ''.tap do |base58_val|
    while int_val > 0
      int_val, mod = int_val.divmod(BASE)
      base58_val.prepend ALPHABET[mod]
    end
  end
end

def int_to_uuid(int_val)
  base16_val = int_val.to_s(16)
  [ 8, 4, 4, 4, 12 ].map do |n|
    base16_val.slice!(0...n)
  end.join('-')
end

uuid = "123e4567-e89b-12d3-a456-426655440000"
int_val = uuid.delete('-').to_i(16)
base58_val = int_to_base58(int_val)
int_val2 = base58_to_int(base58_val)
uuid2 = int_to_uuid(int_val2)

printf <<END, uuid, int_val, base_58_val, int_val2, uuid2
Input UUID: %s
Input UUID as integer: %d
Integer encoded as base 58: %s
Integer decoded from base 58: %d
Decoded integer as UUID: %s
END

输出:

Input UUID: 123e4567-e89b-12d3-a456-426655440000
Input UUID as integer: 24249434048109030647017182302883282944
Integer encoded as base 58: 3fEgj34VWmVufdDD1fE1Su
Integer decoded from base 58: 24249434048109030647017182302883282944
Decoded integer as UUID: 123e4567-e89b-12d3-a456-426655440000