Tuesday, March 29, 2016

Short GUID string

Short GUID

C#

Guid.NewGuid().ToString();  
32 characters

AAD5E2E7-AD78-487A-9B37-458B9F1EE897

Convert.ToBase64String(Guid.NewGuid().ToByteArray())
.Substring(0, 22).Replace("/", "_").Replace("+", "-");
22 characters

b14I850fIE6cuyv7VnP6WA

Base64 code encoding is convenient but not optimal, since it is using only 64 codes out of 95 view-able available in ASCII, and is using '/' and '+' that are not URL friendly.
It is using 6 out of 8 bits in a byte, so overhead compared to binary is 22/16 (37%). 
It is still better than typical hex format that takes 32 bytes instead of binary 16 (100% overhead). 

Since whole bytes are used, 22 final bytes can hold 132 bits, 
and GUID is 16 bytes = 128 bits, so there is a 4 extra bits that could be used for checksum. 

Habits 101

We are what we repeatedly do. Excellence, then, is not an act, but a habit.
- Aristotle at BrainyQuote


free class: Habits 101 | Brian Johnson





Arete (moral virtue) - Wikipedia, the free encyclopedia