我正在使用旧数据库,需要开始使用 c# 生成 id。我需要生成与旧 ID 对应的 ID。
我想将 DateTime 转换为 7 digit \ base36 配置。我认为一旦我将代码从base36代码转换为DateTime(再次感谢Joshua),逆向工程就很容易了,但是我仍然遇到困难。
我花了一天的时间试图弄清楚如何从 DateTime 转换为 base36。
下面是从 base36 代码转换为 DateTime 的代码。这段代码似乎工作正常。id 被添加到 sRecid,然后转换为 DateTime。
id Date Time
A7LXZMM 2004-02-02 09:34:47.000
KWZKXEX 2018-11-09 11:15:46.000
LIZTMR9 2019-09-13 11:49:46.000
using System;
using System.Globalization;
using System.Text;
using System.Numerics;
public class Program
{
public static void Main()
{
string sRecid = "KWZKXEX";
char c0 = sRecid[0];
char c1 = sRecid[1];
char c2 = sRecid[2];
char c3 = sRecid[3];
char c4 = sRecid[4];
char c5 = sRecid[5];
char c6 = sRecid[6];
double d6, d5, d4, d3, d2, d1, d0, dsecs;
Console.WriteLine("c0 = " + c0.ToString());
Console.WriteLine();
d6 = Math.Pow(36, 6) * ((Char.IsNumber(c0)) ? (byte)c0 - 48 : (byte)c0 - 55);
d5 = Math.Pow(36, 5) * ((Char.IsNumber(c1)) ? (byte)c1 - 48 : (byte)c1 - 55);
d4 = Math.Pow(36, 4) * ((Char.IsNumber(c2)) ? (byte)c2 - 48 : (byte)c2 - 55);
d3 = Math.Pow(36, 3) * ((Char.IsNumber(c3)) ? (byte)c3 - 48 : (byte)c3 - 55);
d2 = Math.Pow(36, 2) * ((Char.IsNumber(c4)) ? (byte)c4 - 48 : (byte)c4 - 55);
d1 = Math.Pow(36, 1) * ((Char.IsNumber(c5)) ? (byte)c5 - 48 : (byte)c5 - 55);
d0 = Math.Pow(36, 0) * ((Char.IsNumber(c6)) ? (byte)c6 - 48 : (byte)c6 - 55);
dsecs = (d6 + d5 + d4 + d3 + d2 + d1 + d0) / 50;
DateTime dt = new DateTime(1990, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
dt = dt.AddSeconds(dsecs).ToLocalTime();
Console.WriteLine("d6 = " + d6.ToString());
Console.WriteLine("d5 = " + d5.ToString());
Console.WriteLine("d4 = " + d4.ToString());
Console.WriteLine("d3 = " + d3.ToString());
Console.WriteLine("d2 = " + d2.ToString());
Console.WriteLine("d1 = " + d1.ToString());
Console.WriteLine("d0 = " + d0.ToString());
Console.WriteLine("dsecs = " + dsecs.ToString());
Console.WriteLine("dt = " + dt.ToString());
}
}
这是我遇到问题的代码。
using System;
using System.Globalization;
using System.Text;
using System.Numerics;
public class Program
{
/*
A7LXZMM 2004-02-02 09:34:47.000
KWZKXEX 2018-11-09 11:15:46.000
LIZTMR9 2019-09-13 11:49:46.000
*/
public static void Main()
{
DateTime dt = new DateTime(2004, 02, 02, 09, 34, 47); // Convert this datetime to A7LXZMM
DateTime dtBase = new DateTime(1990, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
double offsetseconds = (DateTime.Now - DateTime.UtcNow).TotalSeconds;
double seconds = ((dt - dtBase).TotalSeconds) * 50;
double d6 = seconds / (Math.Pow(36, 6));
var q6 = d6.ToString().Split('.');
double dQuotient = double.Parse(q6[0]);
double dRemainder = double.Parse(q6[1]);
char c0 = ((dQuotient <= 9) ? (char)(dQuotient + 48) : (char)(dQuotient + 55));
Console.WriteLine("d6 = " + d6.ToString());
Console.WriteLine("dQuotient = " + dQuotient.ToString());
Console.WriteLine("c0 = " + c0.ToString());
Console.WriteLine("");
double d5 = dQuotient / (Math.Pow(36, 5));
var q5 = d5.ToString().Split('.');
dQuotient = double.Parse(q5[0]);
dRemainder = double.Parse(q5[1]);
char c1 = ((dQuotient <= 9) ? (char)(dQuotient + 48) : (char)(dQuotient + 55));
Console.WriteLine("d5 = " + d5.ToString());
Console.WriteLine("dQuotient = " + dQuotient.ToString());
Console.WriteLine("c1 = " + c1.ToString());
Console.WriteLine("");
}
}
代码开始时很好,我可以得到第一个字符(c0),但我在处理下一个字符(c1 以后)时遇到了问题。
在我的示例中,我传递了日期 2004-02-02 09:34:47 并打算返回 A7LXZMM。我哪里错了?
谢谢。