将 Double 转换为 8 字节数组
我想将 Double
变量转换为 8 字节数组,这就是我到目前为止所得到的:
Dim b(0 To 7) As Byte
Dim i As Integer
dim d as double
d = 1 ' for simplicity, I sit the variable "d" to 1
For i = 0 To 7
Call CopyMemory(b(i), ByVal VarPtr(d) + i, 1)
Next i
' b => [0, 0, 0, 0, 0, 0, 240, 63]
我做错了什么?
I want to convert a Double
variable into an 8-bytes array, this is what I've come with so far:
Dim b(0 To 7) As Byte
Dim i As Integer
dim d as double
d = 1 ' for simplicity, I sit the variable "d" to 1
For i = 0 To 7
Call CopyMemory(b(i), ByVal VarPtr(d) + i, 1)
Next i
' b => [0, 0, 0, 0, 0, 0, 240, 63]
What I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要使用循环,使用长度参数:
Don't use a loop, use the length argument:
您不显示声明语句,但可以针对不同用途对 CopyMemory 进行不同的声明。尝试:
You don't show your declare statement, but CopyMemory can be declared differently for different uses of it. Try:
我知道我参加这个聚会迟到了,但是我们有一些使用 CopyMemory 的代码,这些代码曾经快如闪电,然后突然停止了。我认为这是由于 Windows Defender 的更改所致。我有一个简单的解决方法。创建两种类型,一种用于保存双精度型,一种用于保存构成双精度型的 8 个字节。然后使用 LSet 函数将二进制数据从一个复制到另一个。它的运行速度比 CopyMemory 快 6000(!)倍。与 VBA 相比,本机 API 调用现在速度慢得令人难以置信,这真是垃圾,但我们就是这样……
I know I'm late to this party, but we've had some code using CopyMemory that used to be lightening fast, then suddenly it has ground to a halt. I believe this to be due to changes with Windows Defender. I have a simple work around. Create two types, one to hold a double, one to hold the 8 bytes that make up a double. Then use the LSet function to copy the binary data from one to the other. It runs 6000(!) times faster than CopyMemory. Pretty rubbish that a native API call is now incredibly slow compared to VBA, but there we are...