How can I shorten a UUID to a specific length?
问题 I'd like to use a UUID for database records but if I'm using it for the URL I'd like it to be 5 to 8 characters. I know I need to use SecureRandom and base64 , but how do I specify the length I need? 回答1: You can't get a real UUID down to 5-8 characters, as another answer points out, but you can shorten them somewhat. UUIDs are 128-bit integers which works out to 32 hex digits. You can easily store 6 bits per character and cut the length down to 22 characters, which is what base 64 encoding