I want to encode an id to unique strings containing numbers and uppercase letters, like this:
40 => A5TY8
but
41 => Y7HEG
Where the output for 41
is completely different from 40
's A5TY9
.
How to do this?
I want to encode an id to unique strings containing numbers and uppercase letters, like this:
40 => A5TY8
but
41 => Y7HEG
Where the output for 41
is completely different from 40
's A5TY9
.
How to do this?
I wanted to tell you all about digests.. but seeing your comment I think you want something like this
https://github.com/namick/obfuscate_id
or better yet a more generic
https://github.com/patdeegan/integer-obfuscator
you can also encode it with Base64 which is reverisble
http://ruby-doc.org/stdlib-1.9.3/libdoc/base64/rdoc/Base64.html
note that you might want to use urlsafe_encode64
so you won't have /n and stuff in there
so you can do something like
require "base64"
original = 41
converted = Base64.urlsafe_encode64("41")
converted_for_display = converted.tr('^A-Za-z0-9', '')
# => "NDE"
reversed = Base64.urlsafe_decode64(converted)
you can also encrypt the number which should make it unique and reversible, but slower and with more hassle but you can pick whatever key you want