2

I'm trying to format a UUIDv4 into a url friendly string. The typical format in base16 is pretty long and has dashes:

xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx

To avoid dashes and underscores I was going to use base58 (like bitcoin does) so each character fully encode sqrt(58).floor = 7 bits.

I can pack the uuid into binary with:

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

To get 8-bit unsigned integers its:

binary.unpack('C*')

How can i unpack every 7-bits into 8-bit unsigned integers? Is there a pattern to scan 7-bits at a time and set the high bit to 0?

AJcodez
  • 31,780
  • 20
  • 84
  • 118

2 Answers2

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

and back again

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

A handy pattern to reconstruct the uuid format from a template

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"      
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
1

John La Rooy's answer is great, but I just wanted to point out how simple the Base58 algorithm is because I think it's neat. (Loosely based on the base58 gem, plus bonus original int_to_uuid function):

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

Output:

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
Jordan Running
  • 102,619
  • 17
  • 182
  • 182