需要帮助在C#中将十六进制字符串转换为时

时间:2013-04-24 00:45:17

标签: string time hex type-conversion

我遇到了解如何将十六进制值转换为持续时间的问题。

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

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

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

感谢。

1 个答案:

答案 0 :(得分:0)

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