开发者

What's the most compact way of converting 96-bit integer into a string (alternatives to Base64)

开发者 https://www.devze.com 2023-03-24 17:12 出处:网络
As you might have guessed, this is for generating short URLs. What\'s the best way to compact a 96-bit integer into a URL friendly 开发者_StackOverflowshort string? Is Base64 the best way or are the

As you might have guessed, this is for generating short URLs.

What's the best way to compact a 96-bit integer into a URL friendly 开发者_StackOverflowshort string? Is Base64 the best way or are there any alternatives?


I would use base64 for this. It protects you from illegal characters and generates string that 'look' a little like being human-readable.


you could calculate the bit sum


An easy approach is a modified Base 64 encoding. The original Base 64 won't work because it uses the plus (+) and slash (/) character, which have a special meaning in URLs.

But if you replace these characters with a minus (-) and underscore(_) character, you have a nice solution.

If I'm not mistaken, there are 65 or 66 URL-safe characters. So a modified Base 64 comes pretty close to the optimum.


What about converting it to hexadecimal?

0

精彩评论

暂无评论...
验证码 换一张
取 消