C# 流水號序列號生成 [0,9]~[A,Z]
阿新 • • 發佈:2022-12-02
public class SerialNum { public SerialNum() { m_NumByteArr = new byte[4] { 48, 48, 48, 48 }; //m_NumByteArr = new byte[4] { 90, 90, 90, 90 }; ExcludeChar = new List<byte> { (byte) 'A', (byte) 'E', (byte) 'I', (byte) 'O', (byte) 'U' }; } public List<byte> ExcludeChar { get; } /**0~9 十進位制ASCII碼[48, 57] **/ /**A~Z 十進位制ASCII碼[65, 90]**/ public string Number { get { return Encoding.Default.GetString(m_NumByteArr); } } protected byte[] m_NumByteArr; public bool AddOne() { m_NumByteArr[3] = AddChar(m_NumByteArr[3]); if (m_NumByteArr[3] > (byte)'Z') { m_NumByteArr[2] = AddChar(m_NumByteArr[2]); m_NumByteArr[3] = (byte)'0'; } if (m_NumByteArr[2] > (byte)'Z') { m_NumByteArr[1] = AddChar(m_NumByteArr[1]); m_NumByteArr[2] = (byte)'0'; } if (m_NumByteArr[1] > (byte)'Z') { m_NumByteArr[0] = AddChar(m_NumByteArr[0]); m_NumByteArr[1] = (byte)'0'; } return m_NumByteArr[0] <= (byte)'Z'; } public bool SubOne() { m_NumByteArr[3] = SubChar(m_NumByteArr[3]); if (m_NumByteArr[3] < (byte)'0') { m_NumByteArr[2] = SubChar(m_NumByteArr[2]); m_NumByteArr[3] = (byte)'Z'; } if (m_NumByteArr[2] < (byte)'0') { m_NumByteArr[1] = SubChar(m_NumByteArr[1]); m_NumByteArr[2] = (byte)'Z'; } if (m_NumByteArr[1] < (byte)'0') { m_NumByteArr[0] = SubChar(m_NumByteArr[0]); m_NumByteArr[1] = (byte)'Z'; } return m_NumByteArr[0] >= (byte)'0'; } private byte AddChar(byte ch) { while (true) { ch++; if (!ExcludeChar.Contains(ch)) { break; } } if (ch == ((byte)'9' + 1)) { ch = (byte)'B'; } return ch; } private byte SubChar(byte ch) { while (true) { ch--; if (!ExcludeChar.Contains(ch)) { break; } } if (ch == ((byte)'A' - 1)) { ch = (byte)'9'; } return ch; } }