我正在尝试将datetime转换为十六进制并将其发送到查询字符串中的另一个页面,我试图再次将十六进制转换为日期时间。我已将datetime转换为HEX,如此
private string DateToHex(DateTime theDate)
{
string isoDate = theDate.ToString("yyyyMMddHHmmss");
string xDate = (long.Parse(isoDate)).ToString("x");
string resultString = string.Empty;
for (int i = 0; i < isoDate.Length - 1; i++)
{
int n = char.ConvertToUtf32(isoDate, i);
string hs = n.ToString("x");
resultString += hs;
}
return resultString;
}
通过将Datetime转换为HEx,我得到了这个32303134303631313136353034
,而在另一个页面中,我试图将十六进制转换为Date时间,就像这样
private DateTime HexToDateTime(string hexDate)
{
int secondsAfterEpoch = Int32.Parse(hexDate, System.Globalization.NumberStyles.HexNumber);
DateTime epoch = new DateTime(1970, 1, 1);
DateTime myDateTime = epoch.AddSeconds(secondsAfterEpoch);
return myDateTime;
}
我试过这个将HEX转换为DateTime
string sDate = string.Empty;
for (int i = 0; i < hexDate.Length - 1; i++)
{
string ss = hexDate.Substring(i, 2);
int nn = int.Parse(ss, NumberStyles.AllowHexSpecifier);
string c = Char.ConvertFromUtf32(nn);
sDate += c;
}
CultureInfo provider = CultureInfo.InvariantCulture;
CultureInfo[] cultures = { new CultureInfo("fr-FR") };
return DateTime.ParseExact(sDate, "yyyyMMddHHmmss", provider);
它显示了这样的Value was either too large or too small for an Int32.
。肯定会有任何解决方案。
任何解决方案都确定有待评估
答案 0 :(得分:3)
DateTime
值已在内部存储为long
,因此您无需绕道创建long
值。您可以获取内部值并将其格式化为十六进制字符串:
private string DateToHex(DateTime theDate) {
return theDate.ToBinary().ToString("x");
}
将其转换回来非常简单:
private DateTime HexToDateTime(string hexDate) {
return DateTime.FromBinary(Convert.ToInt64(hexDate, 16));
}
注意:这也保留了DateTime
值包含的时区设置,以及低至1/10000秒的完整精度。
答案 1 :(得分:1)
我可以发现两个逻辑错误。
您的DateToHex例程忽略了最后一个字符。它应该是
private string DateToHex(DateTime theDate)
{
string isoDate = theDate.ToString("yyyyMMddHHmmss");
string resultString = string.Empty;
for (int i = 0; i < isoDate.Length ; i++) // Amended
{
int n = char.ConvertToUtf32(isoDate, i);
string hs = n.ToString("x");
resultString += hs;
}
return resultString;
}
从十六进制转换为字符串的例程应该一次推进两个字符,即
string hexDate = DateToHex(DateTime.Now);
string sDate = string.Empty;
for (int i = 0; i < hexDate.Length - 1; i += 2) // Amended
{
string ss = hexDate.Substring(i, 2);
int nn = int.Parse(ss, NumberStyles.AllowHexSpecifier);
string c = Char.ConvertFromUtf32(nn);
sDate += c;
}
CultureInfo provider = CultureInfo.InvariantCulture;
CultureInfo[] cultures = { new CultureInfo("fr-FR") };
return DateTime.ParseExact(sDate, "yyyyMMddHHmmss", provider);
答案 2 :(得分:0)
试试这个。我希望这能解决你的目的。我测试了它,似乎工作正常。
将其转换为DateTime-&gt;与字符串&GT;六角
string input = DateTime.Now.ToString("yyyyMMddHHmmss");
string hexValues = "";
int value = 0;
char[] values = input.ToCharArray();
foreach (char letter in values)
{
// Get the integral value of the character.
value = Convert.ToInt32(letter);
// Convert the decimal value to a hexadecimal value in string form.
hexValues += String.Format("{0:X}", value);
hexValues += " ";
}
现在将其再次转换为HEX-&gt;与字符串&GT;日期时间
string stringValue = "";
string[] hexValuesSplit = hexValues.Split(' ');
foreach (String hex in hexValuesSplit)
{
// Convert the number expressed in base-16 to an integer.
if (hex != "")
{
value = Convert.ToInt32(hex, 16);
// Get the character corresponding to the integral value.
stringValue += Char.ConvertFromUtf32(value);
}
}
DateTime dt = DateTime.ParseExact(stringValue, "yyyyMMddHHmmss", null);
答案 3 :(得分:0)
从十六进制转换为时间。
输入:0060CE5601D6CE01 输出:31-10-2013 06:20:48
string hex1;
string[] hex = new string[16];
hex[0] = hex1.Substring(0, 2);
hex[1] = hex1.Substring(2, 2);
hex[2] = hex1.Substring(4, 2);
hex[3] = hex1.Substring(6, 2);
hex[4] = hex1.Substring(8, 2);
hex[5] = hex1.Substring(10, 2);
hex[6] = hex1.Substring(12, 2);
hex[7] = hex1.Substring(14, 2);
//WE DONOT NEED TO REVERSE THE STRING
//CONVERTING TO INT SO WE CAN ADD TO THE BYTE[]
int[] decValue = new int[8];
for (int i = 0; i < 8; i++)
{
decValue[i] = Convert.ToInt32(hex[i], 16);
}
//CONVERTING TO BYTE BEFORE WE CAN CONVERT TO UTC
byte[] timeByte = new byte[8];
for (int i = 0; i < 8; i++)
timeByte[i] = (byte)decValue[i];
DateTime convertedTime = ConvertWindowsDate(timeByte);
textBox7.Text = convertedTime.ToString();
}
public static DateTime ConvertWindowsDate(byte[] bytes)
{
if (bytes.Length != 8) throw new ArgumentException();
return DateTime.FromFileTimeUtc(BitConverter.ToInt64(bytes, 0));
}