开发者_高级运维I have to manage an ID of some objects. I need these ID be unique. I have the constraint that these ID can't be too long in term of digits required
Is base64 is a nice way to reduce the number of digits required to encoding an ID ?
EDIT:
langage : c++
data type : integer , then convert in a std::string
Each character in Base64 can represent 6 bits, so divide your ID length by 6 to see how many characters it will be. Binary data is 8 bits per byte so it will always be shorter, but the bytes won't all be readable.
Base64 will make the ID readable, but it still won't be good if the ID needs to be hand entered, like a key. For that you'll want to restrict the character set further.
Base64 is a nice way to transport binary data over ASCII. It doesn't usually decrease the size of anything. In my experience it increases it by 66% 33% (thanks for the correction).
If you care just about the length of the output string and not the actual byte size. Then by converting from decimal numeric system (base 10) to any numerical system with base higher then 10 the output string will be shorter see example here http://www.translatorscafe.com/cafe/units-converter/numbers/calculator/octal-to-decimal/
for example in their case decimal 9999999999 <- 10 chars long in base 32 numerical system will be 4LDQPDR <- 7 chars long
with up to 95 printable ascii charecters you could use your own base 95 numerical system and get even shorter string
used this approach in one of my projects and it was enough to squeeze "long" numerical ids in short string fields
精彩评论