Is there any clever way to encode the uuid string with base64?

I’ve got the uuid string using uuid.rb just like this:

ree-1.8.7-2010.02 > uuid = UUID.new()
=> MAC: 34:15:9e:33:f8:3c Sequence: 19368
ree-1.8.7-2010.02 > uuid.generate(:compact)
=> “d52553808dda012d4ba834159e33f83c”

But the string is too long for me, i want to encode it with base64.
Is any clever way to do this?

On Aug 19, 2010, at 11:19 AM, Cyril.Liu wrote:

I’ve got the uuid string using uuid.rb just like this:

ree-1.8.7-2010.02 > uuid = UUID.new()
=> MAC: 34:15:9e:33:f8:3c Sequence: 19368
ree-1.8.7-2010.02 > uuid.generate(:compact)
=> “d52553808dda012d4ba834159e33f83c”

But the string is too long for me, i want to encode it with base64.
Is any clever way to do this?

If anything, base64 encoding will make it longer.

What are you trying to accomplish?

cr

Chuck R. wrote:

On Aug 19, 2010, at 11:19 AM, Cyril.Liu wrote:

But the string is too long for me, i want to encode it with base64.
Is any clever way to do this?
If anything, base64 encoding will make it longer.

Not if he converts it to binary first.

Here’s one I made earlier, not base64 though; this is base91.
It turns UUIDs into 20 printable ASCII bytes.
You can change the reference string to whatever you like.
I use sysuuid (gem install it), but you make get uuids elsewhere.

require ‘sysuuid’

class Integer
def base(b)
self < b ? [self] : (self/b).base(b) + [self%b]
end
end

If we only used upper, lower and digits (base 62), it’d need 22

characters and be more easily copy-pastable
BASE91 =
‘!#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~’

uuid = sysuuid.gsub(/-/,’’).hex
uuid20 = uuid.base(91).map{|i| BASE91[i].chr }’’
uuid_again = uuid20.split(//).inject(0){|i,e| i
91 + BASE91.index(e[0])
}

Clifford H…

Cyril.Liu wrote:

I’ve got the uuid string using uuid.rb just like this:

ree-1.8.7-2010.02 > uuid = UUID.new()
=> MAC: 34:15:9e:33:f8:3c Sequence: 19368
ree-1.8.7-2010.02 > uuid.generate(:compact)
=> “d52553808dda012d4ba834159e33f83c”

But the string is too long for me, i want to encode it with base64.
Is any clever way to do this?

str = “d52553808dda012d4ba834159e33f83c”
puts [[str].pack(“H*”)].pack(“m”)

=> “1SVTgI3aAS1LqDQVnjP4PA==\n”

it’s cool! thx Clifford :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs