Tom van Stiphout <to*****@no.spam.cox.net> wrote in
news:nm********************************@4ax.com:
On 21 Jan 2004 03:36:31 GMT, Lyle Fairfield
<Mi************@Invalid.Com> wrote:
That seems risky, because of the byte layout, which is not as innocent
people (not sure that includes you :-)) might expect.
As a test, use the longint &HAABBCCDD&, CopyMemory it, and observe
you're not getting the characters for &HAA, &HBB etc in that order.
-Tom.
My not so carefully checked or documented code ...
I start with strings here only because I want to test to be sure I get back
to the right strings. Of course later, I will start directly from the
doubles.
Option Base 0
Option Explicit
Private Declare Sub CopyMemory _
Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, Source As Any, ByVal Length As Long)
Public Sub test()
Dim aByteArrays() As Byte
Dim aBytes() As Byte
Dim aDoubles(0 To 2) As Double
Dim aStrings(0 To 2) As String
Dim lngIterator As Long
Dim lngOutsideIterator As Long
Dim lngInsideIterator As Long
Dim strDummy As String
Dim strMessage As String
aStrings(0) = "QRSTUVWX"
aStrings(1) = "IJKLMNOP"
aStrings(2) = "ABCDEFGH"
'Store the 8 bytes in a Byte Array
ReDim aByteArrays(8 * (UBound(aStrings) - LBound(aStrings) + 1))
For lngIterator = 0 To 2
aBytes = StrConv(aStrings(lngIterator), vbFromUnicode)
CopyMemory aByteArrays(8 * lngIterator), aBytes(0), 8
Next
' first check
' do we have the right bytes in the byte arrays
strMessage = "Original Strings" & vbTab
For lngOutsideIterator = 0 To 2
strDummy = ""
For lngInsideIterator = 0 To 7
strDummy = strDummy & Chr$(aByteArrays(lngInsideIterator +
lngOutsideIterator * 8))
Next lngInsideIterator
strMessage = strMessage & strDummy & vbTab
Next lngOutsideIterator
' we get the original three strings
' so it seems we can store these chars as bytes
' now to create the doubles
strMessage = strMessage & vbNewLine & "AS Doubles" & vbTab
For lngIterator = 0 To 2
CopyMemory aDoubles(lngIterator), aByteArrays(8 * lngIterator), 8
strMessage = strMessage & aDoubles(lngIterator) & vbTab
Next lngIterator
' we should now have the double equivalent of our strings
' this will show us the double for which the bytes corresponding
' to "ABCDEFG" ar stored
' now reverse the process to convert these back into Byte Arrays
For lngIterator = 0 To 2
CopyMemory aByteArrays(8 * lngIterator), aDoubles(lngIterator), 8
Next lngIterator
'and convert those byte arrays back to Strings
strMessage = strMessage & vbNewLine & "Back to Strings" & vbTab
For lngIterator = 0 To 2
CopyMemory aBytes(0), aByteArrays(8 * lngIterator), 8
aStrings(lngIterator) = StrConv(aBytes, vbUnicode)
strMessage = strMessage & aStrings(lngIterator) & vbTab
Next lngIterator
' OK let's sort the string array
With WizHook
.Key = 51488399
.SortStringArray aStrings
End With
' lets check and see if it was sorted correctly
strMessage = strMessage & vbNewLine & "Strings Sorted" & vbTab
For lngIterator = 0 To 2
strMessage = strMessage & aStrings(lngIterator) & vbTab
Next lngIterator
' so far ... so good
' Now
' lets convert the strings back to doubles
' put the 8 bytes from each string back into the byte arrays
For lngIterator = 0 To 2
aBytes = StrConv(aStrings(lngIterator), vbFromUnicode)
CopyMemory aByteArrays(8 * lngIterator), aBytes(0), 8
Next
' and put the bytes back into the double storage
strMessage = strMessage & vbNewLine & "Doubles Sorted" & vbTab
For lngIterator = 0 To 2
CopyMemory aDoubles(lngIterator), aByteArrays(8 * lngIterator), 8
strMessage = strMessage & aDoubles(lngIterator) & vbTab
Next lngIterator
' are they sorted
strMessage = strMessage & vbNewLine
If (aDoubles(0) <= aDoubles(1)) Then
strMessage = strMessage & vbNewLine & aDoubles(0) & " <= " &
aDoubles(1)
Else
strMessage = strMessage & vbNewLine & aDoubles(0) & " IS NOT <= " &
aDoubles(1)
End If
If (aDoubles(1) <= aDoubles(2)) Then
strMessage = strMessage & vbNewLine & aDoubles(1) & " <= " &
aDoubles(2)
Else
strMessage = strMessage & vbNewLine & aDoubles(1) & " IS NOT <= " &
aDoubles(2)
End If
strMessage = strMessage & vbNewLine & vbNewLine _
& "Yes, I know I did a million unnecessary things " _
& vbNewLine _
& "and you could probably figure out why!"
MsgBox strMessage
End Sub
--
Lyle
(for e-mail refer to
http://ffdba.com/contacts.htm)