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

C

Cyril.Liu

[Note: parts of this message were removed to make it a legal post.]

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?
 
C

Cyril.Liu

[Note: parts of this message were removed to make it a legal post.]

it's cool! thx Clifford :)

Chuck said:
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.chr }*''
uuid_again = uuid20.split(//).inject(0){|i,e| i*91 + BASE91.index(e[0]) }

Clifford Heath.
 
B

Brian Candler

Cyril.Liu said:
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"
 
C

Clifford Heath

Chuck said:
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.chr }*''
uuid_again = uuid20.split(//).inject(0){|i,e| i*91 + BASE91.index(e[0]) }

Clifford Heath.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top