zoukankan      html  css  js  c++  java
  • C#实现DateTime与byte[]相互转换

    public static DateTime BytesToDateTime(byte[] bytes, int offset)   
          {   
              if (bytes != null)   
              {   
                  long ticks = BitConverter.ToInt64(bytes, offset);   
                  if (ticks < DateTime.MaxValue.Ticks && ticks > DateTime.MinValue.Ticks)   
                  {   
                      DateTime dt = new DateTime(ticks);   
                      return dt;   
                  }   
              }   
                  return new DateTime();   
          }   
             
             
             
          public static byte[] DateTimeToBytes(DateTime dt)   
          {   
              return BitConverter.GetBytes(dt.Ticks); 
          }

          由上述的方法,想必大家应该看得出来,实现DateTime与Byte[]的转换机制,需要以long类型的DateTime.Ticks作为类型转换的中介

  • 相关阅读:
    poj 3159 Candies
    强连通分量——Tarjan算法
    nyoj 次方求模
    nyoj 快速查找素数
    nyoj 光棍节的快乐
    拓扑排序
    快速幂取模
    nyoj 最大素因子
    素数打表
    nyoj 数的长度
  • 原文地址:https://www.cnblogs.com/94cool/p/2696579.html
Copyright © 2011-2022 走看看