[Webinar] Streamline your web hosting managementRegister Today

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1502

# Convert ByteArray to Double variable.

I'm using WinSock control in VB6. The app receives an array of bytes with
two double definition (Event DataArrival).

How can i convert this Byte-Array to a Double variable?

Here is the first double definition array:
coord(0)=184
coord(1)=30
coord(2)=133
coord(3)=235
coord(4)=81
coord(5)=184
coord(6)=41
coord(7)=64

The double number is 12.86
0
CarlosRodrigues
• 2
1 Solution

Commented:
'like this
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(ByVal Destination As Long, _
ByVal Source As Long, _
ByVal Length As Long)

Sub Main()
Dim coord(0 To 7) As Byte
Dim dbl As Double
coord(0) = 184
coord(1) = 30
coord(2) = 133
coord(3) = 235
coord(4) = 81
coord(5) = 184
coord(6) = 41
coord(7) = 64

Call CopyMemory(VarPtr(dbl), VarPtr(coord(0)), 8)
Debug.Print dbl
End Sub
0

Commented:
Beautiful!

PS - I guess that if you get multiple doubles this way (say 2) to can get subsequent doubles by adding 8?

Dim coord(0 To 15) As Byte
Dim dbl1 As Double
Dim dbl2 As Double

coord(0) = 184
coord(1) = 30
coord(2) = 133
coord(3) = 235
coord(4) = 81
coord(5) = 184
coord(6) = 41
coord(7) = 64
coord(8) = 184
coord(9) = 30
coord(10) = 133
coord(11) = 235
coord(12) = 81
coord(13) = 184
coord(14) = 41
coord(15) = 64

Call CopyMemory(VarPtr(dbl1), VarPtr(coord(0)), 8)
Call CopyMemory(VarPtr(dbl2), VarPtr(coord(8)), 8)
etc..
Debug.Print dbl1
Debug.Print dbl2
0

Author Commented:
Thanks. Your answer solves my problem but, canĀ“t VB do that for me?
Try this code.

dim someText as string
dim varByte() as Byte
dim someTextConvertAutomatic as string

someText="Hello!"

varByte=someText

'Visual Basic converts this variable automaticaly. How can i do this to a Double variable

someTextConvertAutomatic = varByte
debug.print someTextConvertAutomatic
0

Commented:
Carlos, I'm afraid you can't. The developers of VB, whatever their motivation might be, decided to incorporate the bytearray to string conversion (and vice versa) in the VB language, but it really is an exception to the rule. Generally it is impossible to do stuff like this in native VB.
0

## Featured Post

• 2
Tackle projects and never again get stuck behind a technical roadblock.