2

(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?

Community
  • 1
  • 1
Balbo
  • 75
  • 1
  • 10

1 Answers1

6

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.

Deanna
  • 23,876
  • 7
  • 71
  • 156
  • Thanks, this is working fine: `Sub Byte2Double(byIN() As Byte, ByRef dOUT As Double)` `CopyMemory dOUT, byIN(0), LenB(dOUT)` `End Sub` The byte array is created with the same routine mentioned and in my case there's no need to error-check since the caller will do the checks. – Balbo Apr 03 '13 at 15:34
  • 1
    @Balbo Never rely on the caller to sanitise your input :) – Deanna Apr 03 '13 at 16:44