简体   繁体   English

使用字母和数字生成序列号

[英]Generate serial number using letters and digits

I'm developing an application for taking orders in C# and DevExpress, and I need a function that generates a unique order number. 我正在开发一个用于在C#和DevExpress中接受订单的应用程序,并且需要一个生成唯一订单号的函数。 The order number must contain letters and digits and has a length of 20 .. I've seen things like Guid.NewGuid() but I don't want it to be totally random, nor to be just an auto increment number .. 订单号必须包含字母和数字,且长度为20 ..我已经看到了Guid.NewGuid()之类的东西,但我不希望它是完全随机的,也不希望是一个自动递增的数字..

Can anyone help? 有人可以帮忙吗? even if it's a script in a different language, I need ideas desperately :) 即使它是另一种语言的脚本,我也急切需要想法:)

You can create type of your own . 您可以创建自己的类型。 lets say yyyyMMddWWW-YYY-XXXXXXX where WWW is the store number, YYY the cashier id XXXXXXX is a hexadecimal number ( -> maybe an actual autoincrement number that you turn it into hex ) . 假设yyyyMMddWWW-YYY-XXXXXXX,其中WWW是商店编号,YYY收银员ID XXXXXXX是十六进制数字(->也许是您将其变为十六进制的实际自动递增数字)。 This is just an idea . 这只是一个想法。 Im afraid you have to decide by the elements of your system how it will be . 恐怕您必须根据系统的要素来决定它的状态。

edited : also if you can apply a check digit algorithm on it will also help in avoiding mistakes 编辑:同样,如果您可以对其应用校验数字算法,也将有助于避免错误

Two different methods: 两种不同的方法:

  • Create MD5 or SHA1 hash of current time 创建当前时间的MD5或SHA1哈希
  • Hash of increment number 增量编号的哈希

Like Oded suggested, Guid is not random (well, not if you have a network card). 就像Oded建议的一样,Guid不是随机的(嗯,如果您有网卡,则不是)。 It's based on time and location coordinates. 它基于时间和位置坐标。 See Raymond Chens blog post for a detailed explanation. 有关详细说明,请参见Raymond Chens博客文章
You are best off using an auto incremented int for order ids. 最好为订单ID使用自动递增的int。 I don't understand why you wouldn't want to use it or failing that a Guid? 我不明白您为什么不想使用它或使Guid失败?

I can't think of any way other then an auto id to maintain uniqueness and represent the order of your different orders in your system. 除了自动ID来保持唯一性并代表系统中不同订单的顺序外,我想不出其他任何方法。

One thought comes to mind. 我想到一个想法。
Take the DateTime.Now.Ticks convert it to hexadecimal string. 将DateTime.Now.Ticks转换为十六进制字符串。
Voila, String.Format("{0:X}", value); 瞧, String.Format("{0:X}", value);
If not long enough , you said you need 20 digits, you can always pad with zeros. 如果不够长,您说您需要20位数字,则始终可以填充零。

  1. Get the mother board ID 获取主板ID
  2. Get the hdd ID 获取硬盘ID
  3. Merge it by any way 以任何方式合并
  4. Add your secret code 添加您的密码
  5. Apply MD5 应用MD5
  6. Apply Base54 应用Base54

Result: the serial code which is linked to the currect client PC :) 结果:链接到当前客户端PC的序列号:)

My two cents. 我的两分钱。

If you need ideas then take a look at the Luhn and Luhn mod N algorithms. 如果您需要创意,请查看LuhnLuhn mod N算法。

While these algorithms are not unique code generators, they may give you some ideas on how to generate codes that can be validated (such that you could validate the code for correctness before sending it off to the database). 尽管这些算法不是唯一的代码生成器,但它们可能会为您提供一些有关如何生成可以验证的代码的想法(这样,您就可以在将代码发送到数据库之前验证代码的正确性)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM