|
-
Jan 18th, 2002, 11:11 AM
#1
Thread Starter
New Member
Charcters to Doubles
I'm receiving an array of bytes (characters) from a winsock connection, groups of 8 of these bytes represent doubles.
How can I take these 8 bytes and form the appropriate double?
And conversly, how can I breakdown a double into its 8 bytes to send?
Thanks
-
Jan 18th, 2002, 09:14 PM
#2
strconv with unicode and from unicode is pretty good at converting bytes to chars.
BUT... Not sure if thats what you want.
Last edited by NotLKH; Jan 18th, 2002 at 09:19 PM.
-
Jan 18th, 2002, 11:28 PM
#3
Ill assume C++. There is a way to cast from floats to DWORDs, so let me see if can find something for you...Here is the code to go from a float to a DWORD:
So, from this:
Code:
*(double*)(&(__int64)c[0])
where c is the byte array. __in64 is a 64 bit data type, which is 8 bytes long, so, by casting to __int64, you tell the compiler to use the following 8 bytes for your value. You take the address of this value, and cast it to a double pointer, which tells the compiler that it should be looking for a double at that address (also 8 bytes, but you knew that). Finally, you dereference the pointer to get the 8 bytes as a double. Here is a little helper function for you:
Code:
double BArrayToDouble(char* c) {
return *(double*)(&(__int64)c[0]);
}
Z.
-
Jan 21st, 2002, 02:31 AM
#4
Registered User
Just use copy memory:
VB Code:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal lpDest As Long, ByVal lpSource As Long, ByVal nBytes As Long)
Private Sub Command1_Click()
Dim x As Double, y As Double, ba() As Byte, i As Long
x = 3.14159
ReDim ba(1 To Len(x))
CopyMemory VarPtr(ba(1)), VarPtr(x), LenB(x)
For i = 1 To UBound(ba)
Debug.Print ba(i)
Next
CopyMemory VarPtr(y), VarPtr(ba(1)), LenB(x)
Debug.Print "y: " & y
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|