如何在c#中转换十六进制到日期时间

本文关键字:十六进制 日期 时间 转换 | 更新日期: 2023-09-27 18:08:02

我正试图将日期时间转换为十六进制并将其发送到查询字符串中的另一个页面,我正试图再次将十六进制转换为日期时间。我把日期时间转换成十六进制,像这样

    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;
    }

通过将日期时间转换为十六进制,我得到了这样的32303134303631313136353034,在另一个页面中,我试图将十六进制转换为日期时间,像这样

    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转换为日期时间

        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.这样的结构。任何解决方案都是值得赞赏的。

如何在c#中转换十六进制到日期时间

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秒的完整精度。

我可以发现两个逻辑错误。

你的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);

试试这个。我希望这能解决你的问题。我已经测试过了,它似乎工作得很好。将其转换为DateTime-> string-> Hex

        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-> string-> DateTime

        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);

将十六进制转换为时间。

输入: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));
    }