将 8 字节数组转换为 Double [英] Convert 8-bytes array into Double

查看:114
本文介绍了将 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.

这篇关于将 8 字节数组转换为 Double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆