将 8 字节数组转换为 Double

问题描述:

(谈论 Visual Basic 6)

(Talking about Visual Basic 6)

我找到了如何将 Double 转换为 8 字节数组,但反之则不然.

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

在我开始尝试编码之前,是否有一些例行程序可以做到(如链接问题中描述的CopyMemory")?在这种情况下可以使用CopyMemory"吗?

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?

使用与您链接的答案相同的代码,但交换源和目标:

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

我跳过了这个例子的任何错误检查,但你需要确保你的字节数组实际上是 8 个字节长,否则你会得到访问冲突.

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.