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

Discussion in 'Ruby' started by Cyril.Liu, Aug 19, 2010.

  1. Cyril.Liu

    Cyril.Liu Guest

    [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?
     
    Cyril.Liu, Aug 19, 2010
    #1
    1. Advertising

  2. Cyril.Liu

    Cyril.Liu Guest

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

    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.chr }*''
    > uuid_again = uuid20.split(//).inject(0){|i,e| i*91 + BASE91.index(e[0]) }
    >
    > Clifford Heath.
    >
    >
     
    Cyril.Liu, Aug 20, 2010
    #2
    1. Advertising

  3. 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"
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Aug 20, 2010
    #3
  4. 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.chr }*''
    uuid_again = uuid20.split(//).inject(0){|i,e| i*91 + BASE91.index(e[0]) }

    Clifford Heath.
     
    Clifford Heath, Aug 20, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. A
    Replies:
    0
    Views:
    267
  2. Damir Hakimov

    base64.encode and decode not correct

    Damir Hakimov, Aug 16, 2005, in forum: Python
    Replies:
    1
    Views:
    351
  3. Replies:
    0
    Views:
    556
  4. aaragon
    Replies:
    7
    Views:
    2,372
    Paul Brettschneider
    Mar 18, 2008
  5. Replies:
    10
    Views:
    293
    Terry Reedy
    Aug 9, 2012
Loading...

Share This Page