我正在创建一个创建分组数据的C#程序。数据包中的其中一个数据是4字节大小的日期和时间。我这样做:存储日期和时间的另一种数据类型
public byte[] convertDateTimetoHex4bytes(DateTime dt)
{
//Convert date time format 20091202041800
string str = dt.ToString("yyyyMMddhhmmss");
//Convert to Int32
Int32 decValue = Convert.ToInt32(str);
//convert to bytes
byte[] bytes = BitConverter.GetBytes(decValue);
return bytes;
}
但是,似乎我需要使用8字节的数据,因为32位太小(运行时错误)。任何人都可以帮助我,如果有4字节的其他更小的日期时间格式?或者其他方式?
为什么你需要转换为字节数组?你打算如何处理结果? –
您试图遵循的确切格式或规格是什么?为什么你将DateTime格式化为像这样的字符串? – OSborn
可能重复 - http://stackoverflow.com/questions/249760/how-to-convert-unix-timestamp-to-datetime-and-vice-versa –