Conversion help

Last post 06-23-2009 9:55 AM by lmayne. 1 replies.

Sort Posts:

  • Conversion help

    06-23-2009, 7:11 AM
    • Member
      120 point Member
    • lmayne
    • Member since 10-11-2004, 5:17 AM
    • Stafford, UK
    • Posts 36

    Hi all,
    Can someone give me a hand converting this PHP code to VB.NET? I'm completely bamboozled by it all! Basically there is a large string which stores some event data and a timestamp. The timestamp is split into 4 strings, which are converted to an unsigned integer (to make the timestamp) and then converted to a string representation of the date.

    $eventline = "008100146055074000000000028025045070000002020005";
    $n = sscanf($eventline, '%3u%3u%3u%3u%3u', $event, $time0, $time1, $time2, $time3);
    $time = sprintf('%04X', ($time3<<8)|$time2) . sprintf('%04X', ($time1<<8)|$time0);
    $timestamp = date('Y-m-d H:i:s', intval($time, 16));
    echo($timestamp);
    
    (Returns: 2009-06-16 07:39:00)
  • Re: Conversion help

    06-23-2009, 9:55 AM
    • Member
      120 point Member
    • lmayne
    • Member since 10-11-2004, 5:17 AM
    • Stafford, UK
    • Posts 36

    Well, I've managed to emulate exactly what the PHP developer was doing, which means it's a bit hacky:

                Dim intEvent As Integer = CInt(strCurrentEvent.Substring(0, 3))
                Dim intTime0 As Integer = CInt(strCurrentEvent.Substring(3, 3))
                Dim intTime1 As Integer = CInt(strCurrentEvent.Substring(6, 3))
                Dim intTime2 As Integer = CInt(strCurrentEvent.Substring(9, 3))
                Dim intTime3 As Integer = CInt(strCurrentEvent.Substring(12, 3))
                Dim datDate As DateTime = New Date(1970, 1, 1, 1, 0, 0).AddSeconds(CInt("&H" & String.Format("{0:x4}", (intTime3 &lt;< 8) Or intTime2) & String.Format("{0:x4}", (intTime1 << 8) Or intTime0)))
    

    (With extra date offset to convert the time to GMT)

    Can anyone think of a less "hacky" way to do this? 

     

     

Page 1 of 1 (2 items)