Last active
January 8, 2019 14:07
-
-
Save shaunsales/212ff0d6d7986ceacbbb29b49874f380 to your computer and use it in GitHub Desktop.
Convert Base26 to Base10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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