C#獲取當前時間戳
- 獲取當前的毫秒時間戳
public static long Timestamp()
{
long ts = ConvertDateTimeToInt(DateTime.Now);
return ts;
}
public static long ConvertDateTimeToInt(System.DateTime time)
{
long t = (time.Ticks - 621356256000000000) / 10000;
return t;
}
時間戳轉換16進位制byte陣列
- 首先將時間戳轉換16進位制字串
long timeStamp = Public.Timestamp() / 1000;
string str = timeStamp.ToString("X4");
- 16進位制時間戳字串轉換成16進位制byte陣列
public static byte[] strToHexByte(string hexString)
{
hexString = hexString.Replace(":", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
最後使用
long timeStamp = Public.Timestamp() / 1000;
string str = timeStamp.ToString("X4");
byte[] b = Public.strToHexByte(str);
本作品採用《CC 協議》,轉載必須註明作者和本文連結