Skip to content

Instantly share code, notes, and snippets.

@yellis
Last active February 11, 2019 22:51
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yellis/8955467 to your computer and use it in GitHub Desktop.
Save yellis/8955467 to your computer and use it in GitHub Desktop.
Convert 15 Char Salesforce Id to 18 Char Salesforce Id
// Based on algorithm given at http://wp.me/pWWz3-14
static string Convert15CharTo18CharId(string id)
{
if (string.IsNullOrEmpty(id)) throw new ArgumentNullException("id");
if (id.Length == 18) return id;
if (id.Length != 15) throw new ArgumentException("Illegal argument length. 15 char string expected.", "id");
var triplet = new List<string> {id.Substring(0, 5), id.Substring(5, 5), id.Substring(10, 5)};
StringBuilder str = new StringBuilder(5);
string suffix = string.Empty;
foreach (var value in triplet)
{
str.Clear();
var reverse = value.Reverse().ToList();
reverse.ForEach(c => str.Append(Char.IsUpper(c) ? "1" : "0"));
suffix += BinaryIdLookup[str.ToString()];
}
return id + suffix;
}
static readonly Dictionary<string, char> BinaryIdLookup = new Dictionary<string, char>
{
{"00000", 'A'}, {"00001", 'B'}, {"00010", 'C'}, {"00011", 'D'}, {"00100", 'E'}, {"00101", 'F'}, {"00110", 'G'}, {"00111", 'H'},
{"01000", 'I'}, {"01001", 'J'}, {"01010", 'K'}, {"01011", 'L'}, {"01100", 'M'}, {"01101", 'N'}, {"01110", 'O'}, {"01111", 'P'},
{"10000", 'Q'}, {"10001", 'R'}, {"10010", 'S'}, {"10011", 'T'}, {"10100", 'U'}, {"10101", 'V'}, {"10110", 'W'}, {"10111", 'X'},
{"11000", 'Y'}, {"11001", 'Z'}, {"11010", '0'}, {"11011", '1'}, {"11100", '2'}, {"11101", '3'}, {"11110", '4'}, {"11111", '5'}
};
@yellis
Copy link
Author

yellis commented Feb 12, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment