Skip to content

Instantly share code, notes, and snippets.

@shaunsales
Last active January 8, 2019 14:07
Show Gist options
  • Save shaunsales/212ff0d6d7986ceacbbb29b49874f380 to your computer and use it in GitHub Desktop.
Save shaunsales/212ff0d6d7986ceacbbb29b49874f380 to your computer and use it in GitHub Desktop.
Convert Base26 to Base10
// Our example symbol
var symbol = "ABC-1234";
// Use a lookup to avoid calling Math.Pow in a code hot path
int[] Base26Pow = { 1, 26, 676 };
// Get the first 3 digits of our symbol
var base26 = symbol.AsSpan(0, 3);
var base10 = 0;
for (int i=0; i < 3; i++)
{
// Iterate through each digit, derive its value from
// its ASCII code and multiply by its Base26 position
base10 += (base26[i] - 64) * Base26Pow[3 - (i + 1)];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment