using System;
using System.Text;
class Program
{
static void Main(string[] args) {
string mask = "ABCDEFGHJKLMNPQRSTUVWXYZ";
for (int i = 0; i < 1000; i++) {
var str = GetString(mask, i);
var value = GetInteger(mask, str);
if (value != i) {
Console.WriteLine($"** ERROR ** {i}:{value}:{str}");
}
}
Console.WriteLine(IncreaseByStep(mask, "A", 1)); // B
Console.WriteLine(IncreaseByStep(mask, "A", 2)); // C
Console.WriteLine(IncreaseByStep(mask, "A", mask.Length)); // AA
Console.ReadKey();
}
public static string IncreaseByStep(string mask, string str, int step) {
return GetString(mask, GetInteger(mask, str) + step);
}
public static int GetInteger(string mask, string str) {
if (str == null) {
return 0;
}
int baseValue = 1;
int intValue = 0;
for (int i = str.Length - 1; i >= 0; i--) {
char c = str[i];
int index = mask.IndexOf(c);
if (index == -1) {
throw new ArgumentOutOfRangeException();
}
intValue += (index + 1) * baseValue;
baseValue *= mask.Length;
}
return intValue;
}
public static string GetString(string mask, int value) {
if (value < 0) {
throw new ArgumentOutOfRangeException();
}
int baseValue = mask.Length;
var sb = new StringBuilder();
while (value > 0) {
int tmp = (value - 1) / baseValue;
int index = value - tmp * baseValue;
char c = mask[index - 1];
sb.Insert(0, c);
value = tmp;
}
return sb.ToString();
}
}
convert letters increase by step
Hello,
Is there another simple solution if I want to specify the step size?
Example + 4 or + 7
Step 4, I will do this. Convert letters increase by step
string ret = GetNext(RevisionId);
txtOutput.AppendText(Environment.NewLine);
txtOutput.AppendText(ret);
ret = GetNext(ret);
txtOutput.AppendText(Environment.NewLine);
txtOutput.AppendText(ret);
ret = GetNext(ret);
txtOutput.AppendText(Environment.NewLine);
txtOutput.AppendText(ret);
ret = GetNext(ret);
txtOutput.AppendText(Environment.NewLine);
txtOutput.AppendText(ret);
Maybe better, I can say the step size. Works for 2 digits, then some problems wir overflow. Can somebody see the reason and can improve the code?
string GetNewValue(string input, int index)
{
string Mask_range = Mask; // "ABCDEFGHJKLMNPQRSTUVWXYZ";
string result = "";
bool isOverflow = true;
bool isCarryOverLastCharacter = false;
for (int i = input.Length - 1; i >= 0; --i)
{
char c = input[i];
if (isOverflow)
{
int k = Mask_range.IndexOf(c);
if (k + index >= Mask_range.Length - 1)
{
char next_c = result.Length == 0 ? Mask_range[0 + index - Mask_range.Length + k] : Mask_range[0];
result = next_c + result;
isOverflow = true;
isCarryOverLastCharacter = true;
}
else
{
char next_c = isCarryOverLastCharacter ? Mask_range[k + 1] : Mask_range[k + index];
result = next_c + result;
isOverflow = false;
isCarryOverLastCharacter = false;
}
}
else
{
result = c + result;
}
}
if (isOverflow)
result = Mask_range[0] + result;
return result;
}
-
KOZ6.0 6,300 Reputation points
2024-06-13T01:54:04.94+00:00
1 additional answer
Sort by: Most helpful
-
Hongrui Yu-MSFT 785 Reputation points Microsoft Vendor
2024-06-13T05:53:36.88+00:00 Hi,@Noah Aas.
For the issue of IndexOutOfRangeException , you could update the code in GetRawValue as follows:
if (k + index > Mask_range.Length - 1)
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.