Convert 8-bytes array into Double

2020-03-27 03:48发布

问题:

(Talking about Visual Basic 6)

I was able to find how to convert Double into 8-bytes array, but not the viceversa.

Before I start to try to code it, is there some routine to do it (like the "CopyMemory" described in the linked question)? Can the "CopyMemory" be used in this case?

回答1:

Use the same code as the answer you linked to but swap the source and destination around:

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
    ByRef Destination As Any, _
    ByRef Source As Any, _
    ByVal Length As Long)

Function BytesToDbl(ByRef Bytes() As byte) As Double
  Dim D As Double
  CopyMemory D, Bytes(0), LenB(D)
  BytesToDbl = D
End Function

I've skipped any error checking for this example but you'll want to make sure that your byte array is actually 8 bytes long otherwise you'll get an access violation.

Note that this assumes the byte array was created using the linked to question. Floating point values from other sources may well be using a different binary representation which means this will not work.



标签: vb6