本文介绍了将 Double 转换为 8 字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将一个 Double
变量转换为一个 8 字节的数组,这是我到目前为止所得到的:
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]
我做错了什么?
推荐答案
不要使用循环,使用长度参数:
Don't use a loop, use the length argument:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
ByRef Destination As Any, _
ByRef Source As Any, _
ByVal Length As Long)
Sub DblToByte(ByVal D As Double)
Dim Bytes(LenB(D) - 1) As Byte
Dim I As Integer
Dim S As String
CopyMemory Bytes(0), D, LenB(D)
For I = 0 To UBound(Bytes)
S = S & CStr(Bytes(I)) & " "
Next
MsgBox S
End Sub
Private Sub Form_Load()
DblToByte 1
Unload Me
End Sub
这篇关于将 Double 转换为 8 字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!