gpt4 book ai didi

c# - 增加字节数组中的字符

转载 作者:行者123 更新时间:2023-11-30 20:41:31 25 4
gpt4 key购买 nike

我正在用以下内容预填充一个字节数组:

 private static byte[] idBuffer = ASCIIEncoding.Default.GetBytes("A" + DateTime.Now.ToString("yy") + DateTime.Now.DayOfYear + "0000001");

字节数组中的“0000001”部分是我的 ID 部分,每次我调用“增量”方法时,我都想用 ASCII 字符 0-1A-S 递增。

例如,增量序列样本为:

000000S
...
00000S0
00000S1
...
00000SS
...
0000S00
...
0000S99
0000S9A
等等

我在想出正确的算法/状态机来正确递增字符时遇到了一些麻烦。

现在我预填充一个字符表:

private static byte[] charCodes = new byte[] { 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 80, 81, 82, 83};

以及我为此对状态机的粗略尝试,但它只让我获得了第二名:

if (idBuffer[bufPosition] < 83)
{
idBuffer[bufPosition] = charCodes[charCodePosition];
charCodePosition++;
}
else
{
if (idBuffer[bufPosition - 1] == 48)
{
for (int i = bufPosition; i < idBuffer.Length; i++)
idBuffer[i] = 48;

idBuffer[bufPosition - 1] = charCodes[charCodePosition2];
charCodePosition2++;
}
else
{
charCodePosition2++;
idBuffer[bufPosition - 1] = charCodes[charCodePosition2];
}

charCodePosition = 0;
}

我确信有一种我想不到的更优雅的方法来做到这一点 - 理想情况下,如果有人知道如何使用不安全的代码/指针来做到这一点,那就更好了!

最佳答案

其实很简单:

  1. 从最后一个元素开始。
  2. 添加一个。
  3. 如果您现在有 '0'+10 (58),请将其调整为 'A' (65)。
  4. 如果您有除 'T' 之外的任何其他内容(懒得查找 ASCII 代码),您就完成了。
  5. 调整为 '0' (48)。
  6. 向左移动一个元素并从第 2 步开始重复。

关于c# - 增加字节数组中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32261972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com