How to encode a large number (in URL)?

Quite often, you have to encode a large number (for example, 128 or 160 bits) in the URL. For example, many web applications use md5 (random ()) for UUIDs.

If you need to put this value in a URL, the general approach is to simply encode it as a hexadecimal string.

But obviously hexadecimal encoding is not a very hard encoding. What other approaches are good for URLs?

+4
source share
5 answers

You can do even better with base64-url encoding (az, AZ, 0-9, - and _ [see RFC4648, section 5]). RFC4648 covers several different encoding methods (base16, base32 and base64) with several options. In addition, depending on the sparseness of the bits that are set in the number, you could run it through gzip, and then use one of the encoding methods described. Of course, using gzip really depends on how big the number you are going to encode will be.

+3
source

I would use the "Database Identifier" Attribute and "Identifier" and "File Name".

Base 64 uses two character sets.

Data: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ URLs: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_ 

To use base 64, you need to defer your value to a multiple of 3 bytes (24 bits), and then split these 24 bits into 4 6-bit bytes. Each 6-bit value is scanned by the position in the line indicated above.

If all goes well, your final base64 value will always be a multiple of 4 characters and will be decoded back to a multiple of 3 (8 bits) bytes.

Depending on the language you use, many of them have built-in encoding and decoding functions.

+6
source

If you want it to be tough, you can use base-36 encoding (from 0 to Z).

+2
source

Using the base36 hint, I'm currently using something like this (in Python):

 >>> str(base64.b32encode(uuid.uuid1().bytes).rstrip('=')) 'MTB2ONDSL3YWJN3CA6XIG7O4HM' 
0
source

Just use hex. Even if you have to get 8 bits per character, you still use a random sequence of 16-20 characters that no one wants to type or speak. If you cannot set a short identifier, work with search capabilities.

-1
source

All Articles