Click here to Skip to main content
13,662,031 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

12K views
23 bookmarked
Posted 27 Mar 2018
Licenced CPOL

Reducing the string Length of a Guid

, 28 Mar 2018
Rate this:
Please Sign up or sign in to vote.
Need a unique identifier that is smaller than 36 characters? Represent a Guid in a 22 character string.

Introduction

The other day, we were sending some messages to services wherein a messageID was required. We used to create a messageID by using a Guid.NewGuid(), but the one of the receiving services threw an exception. It turned out that this one particular service defined the messageID to be only 30 characters or less. So what to do?

Some of us thought it would be a wise idea to use some form of timestamp in combination with a system identifier. Well, as you can imagine, in an environment that would produce a lot of messages, the chance of that messageID being unique drops dramatically. We wanted to stick with the Guid.

Okay, sticking with the Guid, maybe we can just remove the '-' and truncate the resulting string to 30 characters. Sure, that might work. Still, we are reducing the uniqueness. There must be a better way.

The Solution

We found that converting the Guid (16 bytes) to an ASCII representation using Base64 resulted in a useable and still unique messageID of only 22 characters.

var newGuid = Guid.NewGuid();
var messageID = Convert.ToBase64String(newGuid.ToByteArray());

For example: The Guid 'e6248889-2a12-405a-b06d-9695b82c0a9c' (string length: 36) will get a Base64 representation: 'iYgk5hIqWkCwbZaVuCwKnA==' (string length: 24)

The Base64 representation ends with the '==' characters. You could just truncate these, without any impact on the uniqueness. Leaving you with an identifier of only 22 characters in length.

Proof

Just saying that this works might suffice, but here is a small piece of code that you can run that will prove that the Guid you convert can be converted back to the Guid you started with; ergo, the resulting string is as unique as the starting Guid.

using System;

var newGuid = Guid.NewGuid();

Console.WriteLine($"GUID: {newGuid.ToString()}");
Console.WriteLine($"length: {newGuid.ToString().Length}" );

var base64 = Convert.ToBase64String(newGuid.ToByteArray());
Console.WriteLine($"Base64 representation: {base64}");
Console.WriteLine($"length: {base64.Length}" );

var reconstructedGuid = new Guid(Convert.FromBase64String(base64));
Console.WriteLine($"Restored GUID: {reconstructedGuid.ToString()}");
Console.WriteLine("Restore from Base64 : " + (reconstructedGuid==newGuid?"PASSED": "FAILED"));

Points of Interest

Please have a look at the blogpost Equipping our ASCII Armor by Jeff Atwood for more information on using a Guid in different forms. His blogpost inspired this tip.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Christiaan van Bergen
Software Developer (Senior) NoNerds
Netherlands Netherlands
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionGUID storage optimisation Pin
Fregate2-Apr-18 17:00
memberFregate2-Apr-18 17:00 
QuestionConsider how many swear words this can generate... Pin
Jeff Moden30-Mar-18 8:42
memberJeff Moden30-Mar-18 8:42 
AnswerRe: Consider how many swear words this can generate... Pin
Christiaan van Bergen30-Mar-18 11:09
professionalChristiaan van Bergen30-Mar-18 11:09 
QuestionPlagiarism Pin
geoatif30-Mar-18 5:58
professionalgeoatif30-Mar-18 5:58 
AnswerRe: Plagiarism Pin
Christiaan van Bergen30-Mar-18 10:51
professionalChristiaan van Bergen30-Mar-18 10:51 
GeneralRe: Plagiarism Pin
geoatif31-Mar-18 0:46
professionalgeoatif31-Mar-18 0:46 
QuestionRemoving dash Pin
Francis Grignon30-Mar-18 5:15
memberFrancis Grignon30-Mar-18 5:15 
AnswerRe: Removing dash Pin
Christiaan van Bergen30-Mar-18 5:58
professionalChristiaan van Bergen30-Mar-18 5:58 
NewsMethod was originally explained on StackOverflow just over 6 years ago.... Pin
_andrea30-Mar-18 4:27
member_andrea30-Mar-18 4:27 
GeneralRe: Method was originally explained on StackOverflow just over 6 years ago.... Pin
Christiaan van Bergen30-Mar-18 4:49
professionalChristiaan van Bergen30-Mar-18 4:49 
GeneralRe: Method was originally explained on StackOverflow just over 6 years ago.... Pin
_andrea30-Mar-18 5:27
member_andrea30-Mar-18 5:27 
QuestionT-SQL function versions Pin
tutor30-Mar-18 3:35
membertutor30-Mar-18 3:35 
AnswerRe: T-SQL function versions Pin
Christiaan van Bergen30-Mar-18 4:52
professionalChristiaan van Bergen30-Mar-18 4:52 
GeneralRe: T-SQL function versions Pin
tutor30-Mar-18 5:36
membertutor30-Mar-18 5:36 
QuestionBase85 Pin
degski30-Mar-18 3:00
memberdegski30-Mar-18 3:00 
AnswerRe: Base85 Pin
Christiaan van Bergen30-Mar-18 4:58
professionalChristiaan van Bergen30-Mar-18 4:58 
QuestionGood solution, but beware... Pin
Henrique C.30-Mar-18 2:50
professionalHenrique C.30-Mar-18 2:50 
PraiseRe: Good solution, but beware... Pin
Christiaan van Bergen30-Mar-18 4:59
professionalChristiaan van Bergen30-Mar-18 4:59 
QuestionThe Simple Ideas Are The Best Pin
Patrick Skelton28-Mar-18 23:19
memberPatrick Skelton28-Mar-18 23:19 
GeneralRe: The Simple Ideas Are The Best Pin
Christiaan van Bergen30-Mar-18 5:00
professionalChristiaan van Bergen30-Mar-18 5:00 
PraiseThanks! Just what I needed. Pin
Duane Penzien28-Mar-18 14:51
memberDuane Penzien28-Mar-18 14:51 
GeneralRe: Thanks! Just what I needed. Pin
DaddaDutteBoy30-Mar-18 4:29
memberDaddaDutteBoy30-Mar-18 4:29 
PraiseRe: Thanks! Just what I needed. Pin
Christiaan van Bergen30-Mar-18 5:04
professionalChristiaan van Bergen30-Mar-18 5:04 
GeneralRe: Thanks! Just what I needed. Pin
Christiaan van Bergen30-Mar-18 5:04
professionalChristiaan van Bergen30-Mar-18 5:04 
GeneralMy vote of 5 Pin
BManfred28-Mar-18 7:58
memberBManfred28-Mar-18 7:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180810.1 | Last Updated 29 Mar 2018
Article Copyright 2018 by Christiaan van Bergen
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid