生成十六位字符串(不重复)

类别:.NET开发 点击:0 评论:0 推荐:

using System;

namespace GetSysID
{
 /// <summary>
 /// SysID 的摘要说明。
 /// </summary>
 public class GetSysID
 {
  public GetSysID()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
    public string sysid()
    {
      string sysID,LC,LC1,ss;

      string year,day,hour,minute,sec,msec;
      year = System.DateTime.Now.Year.ToString();     
      day = System.DateTime.Now.DayOfYear.ToString();
      hour = System.DateTime.Now.Hour.ToString();
      minute = DateTime.Now.Minute.ToString();
      sec = DateTime.Now.Second.ToString();
      msec = DateTime.Now.Millisecond.ToString();
     
      if (year.Length == 4)
      {
        year = year.Substring(2,2);
      }
     
      LC1 = "";
      LC1 = LC1 + ChangStr(true,year,"0",2);
      LC1 = LC1 + ChangStr(true,day,"0",3);
      LC1 = LC1 + ChangStr(true,hour,"0",2);
      LC1 = LC1 + ChangStr(true,sec,"0",2);

      long t;
      t = Convert.ToInt64(LC1);

      LC1 = "";
      LC1 = IntToHex(t,8);

      ss = ChangStr(true,sec,"0",2);
      t = Convert.ToInt64(ss);
      LC1 = LC1 + IntToHex(t,2);
      LC1 = LC1 + IntToHex(Convert.ToInt64(msec),3);     

      //64进制
      ss = System.Guid.NewGuid().ToString();     
      LC = ss.Substring(0,8);
      LC = CheckID(LC);     

      LC1 = LC1 + ChangStr(true,LC,"0",8);
      ss = "";
     
      ss = LC1.Substring(1,8);    
      sysID = HexTo36(ss,6);     
      ss= LC1.Substring(8,13);
      sysID = sysID + HexTo36(ss,10);

      return sysID;          
    }
  

   
    //转换为十六进制数据
    private string IntToHex(long num,int len)
    {     
      string lcHexStr,strHex;
      lcHexStr = "0123456789ABCDEF";
      int lMod,len1;     
      lMod = 0;
      strHex = "";

      while (num > 0)
      {
        lMod = (int)(num % 16);
        num = num / 16;
        strHex = lcHexStr.Substring(lMod ,1) + strHex;
      }

      len1 = strHex.Length;
      if (len1 < len )
        strHex = ChangStr(true,strHex,"0",len);
      return strHex;     
    }

    //改变字符串的到指定的长度
    private string ChangStr(bool bISF,string strHex,string subStr,int @len)
    {     
      int len1;
      len1 = len - strHex.Length;
      for (int i = 0; i < len1; i++)
      {
        if (bISF == true)
          strHex = subStr + strHex;
        else
          strHex = strHex + subStr;
      }
      return strHex;
    } 
   
    //检查字符串
    private string CheckID(string str)
    {
      string outStr;
      char charStr;
      int len;     
      outStr = "";

      str = str.ToUpper();
      len = str.Length;

      for (int i =0; i < len; i++ )
      {       
        charStr = str[i];
        if ((((int)charStr >= 48 ) && ((int)charStr <= 59)) || (((int)charStr >= 65 ) && ((int)charStr <= 70)))
        {
          outStr = outStr + charStr;         
        }
        else
          outStr = outStr + IntToHex((int)charStr,0);
      }
      if (outStr.Length > 8)
        outStr = outStr.Substring(0,8);
      return outStr;
    }

    //转换成36进制
    private string HexTo36(string strHex,int len)
    {
      string str62,lc62Str;
      lc62Str = "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      str62 = "";
      int lMod = 0;
      long num1 = 0;

      //转换成十进制
      long lnsHex = 1;
      strHex = strHex.ToUpper();
      int len1;
      len1 = strHex.Length  ;
      while (len1 > 0)
      {
        char ss;
        ss = strHex[(len1 -1)];
        num1 = num1 + (lc62Str.IndexOf(ss) ) * lnsHex;
        if (len1 == 1)
          lnsHex = 16;
        else
          lnsHex = lnsHex * 16;
        len1 --;
        //listBox1.Items.Add(num1.ToString());
      }
     
      //MessageBox.Show(num1.ToString());
      //转换成36进制
      str62 ="";
      while (num1 > 0)
      {       
        lMod = (int)(num1 % 36);
        num1 = num1 / 36;
        str62 = lc62Str.Substring(lMod ,1) + str62;
      }
      len1 = str62.Length;

      if (len1 < len)
      {
        str62 = ChangStr(true,str62,"0",len);
      }
      return str62;
    }
 }
}

本文地址:http://com.8s8s.com/it/it42915.htm