Results 1 to 4 of 4

Thread: Charcters to Doubles

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Posts
    5

    Question 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

  2. #2
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    strconv with unicode and from unicode is pretty good at converting bytes to chars.
    BUT... Not sure if thats what you want.

  3. #3
    Zaei
    Guest
    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:
    Code:
    *(DWORD *)(&Start)
    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.

  4. #4
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Just use copy memory:

    VB Code:
    1. Option Explicit
    2.  
    3.  
    4. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal lpDest As Long, ByVal lpSource As Long, ByVal nBytes As Long)
    5.  
    6. Private Sub Command1_Click()
    7. Dim x As Double, y As Double, ba() As Byte, i As Long
    8.  
    9. x = 3.14159
    10. ReDim ba(1 To Len(x))
    11.  
    12. CopyMemory VarPtr(ba(1)), VarPtr(x), LenB(x)
    13.  
    14. For i = 1 To UBound(ba)
    15.     Debug.Print ba(i)
    16. Next
    17.  
    18. CopyMemory VarPtr(y), VarPtr(ba(1)), LenB(x)
    19.  
    20. Debug.Print "y: " & y
    21.  
    22. 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
  •  



Click Here to Expand Forum to Full Width