Announcement

Collapse
No announcement yet.

How to conver 4 bytes to double floating point?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to conver 4 bytes to double floating point?

    Hi,

    I got a new question to you all

    If I have 8 single bytes in HEX like 00 00 00 40 A6 54 58 41 how can I convert this to double floating point which mean I should have an answer which looks like this; 6378137.000000. I thought the max on both sides of the "." should be 65535 4 bytes but I can see that is not the case?

    I did try the shown sample but the answer is wrong as I test the reply from another software that works;

    Val("&H" & Mid$(sGTData, 45, 8)) & "." & Val("&H" & Mid$(sGTData, 53, 8))

    where sGTData is a long HEX string and I then read from position 45 4 bytes and position 53 4 bytes (in HEX).

    The answer should be 6378137.000000 but I get 64.-1504421823 where the first value is 00000040 and the second A6545841 in HEX. I can se that HEX 40 is 64 but how can the answer be 6378137.000000? Any ideas out there?
    Last edited by Natalie Angel; 16 Dec 2008, 08:04 AM.
    If you don't know what the problem is, then you can't find the answer.

  • #2
    TRY:
    Code:
    UNION Foo 
       D  AS DOUBLE
       B (1 TO 8) AS BYTE
    END UNION
    
    LOCAL DU AS Foo 
      FOR Z = 1 TO 8 
          DU.B(Z) =  VAL ("&H" & MID$(stringin,(Z-1)*2 + 1, 2))
       NEXT
       PRINT FORMAT$(DU.D)
    If that gives you bad answer, try it reversing the bytes of the input string.
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Here is one possibility:
      Code:
      #COMPILE EXE
      #DIM ALL
      
      FUNCTION PBMAIN () AS LONG
         LOCAL d AS DOUBLE
         LOCAL sRev AS STRING
         
         sRev = STRREVERSE$(MKQ$(&h00000040A6545841)) 'reverse the byte order 1st
         d = CVD(sRev)                                'convert it to a double
         ? FORMAT$(d, "0.000000")                     'display it
         
      END FUNCTION
      Last edited by John Gleason; 16 Dec 2008, 05:37 PM. Reason: remove dupe

      Comment


      • #4
        Thank you both.

        I was using
        Sub fnConvertReverse32HEX(sHexString As String)
        sNewData = ""
        For iLoop = 7 To 1 Step -2
        sTmp = Mid$(sHexString, iLoop, 2)
        sNewData = sNewData & sTmp
        Next iLoop
        End Sub

        But I will try John's sample
        If you don't know what the problem is, then you can't find the answer.

        Comment

        Working...
        X