【问题标题】:Need help converting hex string to time in C#需要帮助在 C# 中将十六进制字符串转换为时间
【发布时间】:2013-04-17 09:19:19
【问题描述】:

我无法理解如何将十六进制值转换为持续时间。

以下是我在研究案例中看到的一些示例:

2b3da = 2:44.986
2bf64 = 2:47.868
2c84a = 2:50.074

有人可以帮助了解这些结果是如何达到的吗?

谢谢。

【问题讨论】:

  • 你能提供任何上下文吗?这些数据来自哪里?这可以是任何类型的自定义编码。
  • 可能,这是通过网络发送的 UDP 数据包的一部分。它基本上与赛车游戏有关,并包含完成时间。有人确实成功地转换了这个(这就是我得到这些结果的方式),但到目前为止我无法联系到他。

标签: string time hex type-conversion


【解决方案1】:
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));
}

输入:0060CE5601D6CE01

输出:31-10-2013 06:20:48

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-31
    • 2022-07-22
    • 2016-03-06
    • 2014-06-09
    • 2018-06-06
    • 1970-01-01
    • 2018-01-03
    • 2014-10-27
    相关资源
    最近更新 更多