Try:
string s = "12345678";
string toAlpha = s.Substring(0, 4);
string toDigit = s.Substring(4);
StringBuilder sb = new StringBuilder(8);
foreach (char c in toAlpha)
{
sb.Append((char)('A' + (c - '0')));
}
sb.Append(toDigit);
string output = sb.ToString();
"while gradually increasing from "0000" to "AAAA", "0001" to "AAAB", ... "9999" to "AOUP". How will I know while decoding "AOUP" to "9999". Is any logic to know "AOUP" equal to "9999"."
Just reverse the process:
private string Encode(string numeric)
{
string alphaPart = numeric.Substring(0, 4);
string numPart = numeric.Substring(4);
uint u = uint.Parse(alphaPart);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; i++)
{
sb.Insert(0, (char)('A' + (u % 26)));
u /= 26;
}
sb.Append(numPart);
return sb.ToString();
}
private string Decode(string mixed)
{
string numPart = mixed.Substring(4);
string alphaPart = mixed.Substring(0, 4);
uint u = 0;
foreach (char c in alphaPart)
{
u *= 26;
u += (uint)(c - 'A');
}
return u.ToString("0000") + numPart;
}
Please excuse the indentation - there is a problem with the auto formatter today...