From: "Cyril.Liu" Date: 2010-08-20T12:08:36+09:00 Subject: Re: Is there any clever way to encode the uuid string with base64? --0022150482bb4df648048e389de5 Content-Type: text/plain; charset=ISO-8859-1 it's cool! thx Clifford :) On Fri, Aug 20, 2010 at 9:25 AM, Clifford Heath wrote: > Chuck Remes 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 Heath. > > --0022150482bb4df648048e389de5--