0

I have issues understanding how to convert a hex value to a duration.

Here are some examples I've seen in the case I study:

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

Could someone help to understand how those results are reached ?

Thanks.

MagiKruiser
  • 431
  • 1
  • 6
  • 19
  • Can you provide any context? Where is this data coming from? This could be any kind of custom encoding. – Jason Apr 24 '13 at 01:02
  • Might be, this is part of a UDP packet sent over network. It's related basically to a racing game and contains finish time. Someone did succeed converting this (which is how I came with those results) but I couldn't contact him so far. – MagiKruiser Apr 24 '13 at 01:10

1 Answers1

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

Input : 0060CE5601D6CE01

Output : 31-10-2013 06:20:48

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
Silver
  • 39
  • 4