From: MM on
I am converting big endian to little endian and have written these two
functions:

Function Endian32_UsingString(ByVal n As Long) As Long

Dim s As String
Dim d As String
Dim i As Integer

s = Right$("0000000" & Hex$(n), 8)

For i = 1 To 7 Step 2
d = Mid$(s, i, 2) & d
Next

Endian32_UsingString = CLng("&H" & d)

End Function


Function Endian32_UsingByteArray(ByVal n As Long) As Long

Dim big(3) As Byte
Dim little(3) As Byte

CopyMemory big(0), n, 4

little(3) = big(0)
little(2) = big(1)
little(1) = big(2)
little(0) = big(3)

CopyMemory n, little(0), 4

Endian32_UsingByteArray = n

End Function

Which is preferable and why?

MM
From: Nobody on
"MM" <kylix_is(a)yahoo.co.uk> wrote in message
news:e7bip5tqgk2lsqgumicb9i9r3al9vp9q7j(a)4ax.com...
> Which is preferable and why?

Do a speed test, but the second one is probably faster. There is also an API
function that does this. It's htonl(). This should be faster than both.

Here is a VB6 solution that doesn't use the API and it's probably faster:

Function Endian32LongSwap(ByVal n As Long) As Long
Dim x As Long

' Byte 0
' Special case for negative numbers
If (n And &H80&) <> 0 Then
' Avoid overflow
x = (n And &H7F&) * &H1000000
x = x Or &H80000000
Else
x = (n And &HFF&) * &H1000000
End If
' Byte 1
x = x Or ((n And &HFF00&) * &H100&)
' Byte 2
x = x Or ((n And &HFF0000) \ &H100&)
' Byte 3
' Special case for negative numbers
If (n And &H80000000) <> 0 Then
x = x Or ((n And &H7F000000) \ &H1000000)
x = x Or &H80&
Else
x = x Or ((n And &HFF000000) \ &H1000000)
End If

Endian32LongSwap = x

End Function



From: MM on
On Thu, 11 Mar 2010 13:57:45 -0500, "Nobody" <nobody(a)nobody.com>
wrote:

>"MM" <kylix_is(a)yahoo.co.uk> wrote in message
>news:e7bip5tqgk2lsqgumicb9i9r3al9vp9q7j(a)4ax.com...
>> Which is preferable and why?
>
>Do a speed test, but the second one is probably faster. There is also an API
>function that does this. It's htonl(). ............

Yes, I found that, but it introduces one more "dependency", namely
winsock. Now, I realise that all the PCs my app is ever likely to be
installed will have it, but I'd prefer something that I didn't already
have reference to for other reasons. For example, I never mind about
using a new routine that uses SendMessage because I already use this
call.

I'll write up a speed comparison test later, including the htonl one
and the VB ones. That routine you provided seems quite complicated
with all the ANDs and ORs. I do have access to assembler/C written
shifts in vbhlp32.dll, which is a free non-COM library that I am using
already. Maybe a routine could be built up using shifts, as that is
the approach I've seen in C and Pascal examples.

MM
From: Mike Williams on
"MM" <kylix_is(a)yahoo.co.uk> wrote in message
news:qfqjp55b82u5dlkbpihknnugu21tsdho3g(a)4ax.com...

> [In response to 'Nobody's post] I'll write up a speed
> comparison test later, including the htonl one and the
> VB ones. That routine you provided seems quite
> complicated with all the ANDs and ORs. I do have
> access to assembler/C written shifts in vbhlp32.dll,
> which is a free non-COM library that I am using
> already. Maybe a routine could be built up using
> shifts, as that is the approach I've seen in C and
> Pascal examples.

Shifts would obviously be much less complicated and faster than native VB
code (which has to get around the fact that VB does not provide real shift
methods and the fact that it does not provide unsigned Longs) but generally
any code that calls a declared function to perform a very small task is
slowed down by the overhead of the call itself, and so in many cases the
speed difference is not quite what you might expect it to be.

I've just tested a few different methods of performing your "endian swap",
including a CopyMemory method and the native VB method posted by 'Nobody'
and the call to htonl in "wsock32.dll" method that you mentioned. All tests
were performed as a standard native code compiled exe of course, which is
how your app will almost certainly be compiled.

The Copymemory method used a number of calls to Copymemory and, as expected,
was the slowest of the three. The call to htonl in wsock32.dll was the
fastest, but the straight VB code posted by 'Nobody' came a respectable
second. I'm not sure whether it can be optimized a little further (maybe,
but possibly not) but it is quite useful even as it stands. Here are the
results I got on my own machine (a relatively slow Celeron laptop):

0.215 microseconds for the CopyMemory method
0.056 microseconds for Nobody's VB method
0.025 microseconds for wsock32.dll method

Mike



From: Donald Lessau on
"Nobody" <nobody(a)nobody.com> schrieb im Newsbeitrag
news:O4ZcAzUwKHA.4552(a)TK2MSFTNGP04.phx.gbl...
> "MM" <kylix_is(a)yahoo.co.uk> wrote in message
> news:e7bip5tqgk2lsqgumicb9i9r3al9vp9q7j(a)4ax.com...
>> Which is preferable and why?
>
> Do a speed test, but the second one is probably faster. There is also an
> API function that does this. It's htonl(). This should be faster than
> both.
>
> Here is a VB6 solution that doesn't use the API and it's probably faster:
>
> Function Endian32LongSwap(ByVal n As Long) As Long
> Dim x As Long
>
> ' Byte 0
> ' Special case for negative numbers
> If (n And &H80&) <> 0 Then
> ' Avoid overflow
> x = (n And &H7F&) * &H1000000
> x = x Or &H80000000
> Else
> x = (n And &HFF&) * &H1000000
> End If
> ' Byte 1
> x = x Or ((n And &HFF00&) * &H100&)
> ' Byte 2
> x = x Or ((n And &HFF0000) \ &H100&)
> ' Byte 3
> ' Special case for negative numbers
> If (n And &H80000000) <> 0 Then
> x = x Or ((n And &H7F000000) \ &H1000000)
> x = x Or &H80&
> Else
> x = x Or ((n And &HFF000000) \ &H1000000)
> End If
>
> Endian32LongSwap = x
>
> End Function

This one might be even faster:

Public Function SwapEndian08(ByVal dw As Long) As Long
SwapEndian08 = _
(((dw And &HFF000000) \ &H1000000) And &HFF&) Or _
((dw And &HFF0000) \ &H100&) Or _
((dw And &HFF00&) * &H100&) Or _
((dw And &H7F&) * &H1000000)
If (dw And &H80&) Then SwapEndian08 = SwapEndian08 Or &H80000000
End Function

see http://www.xbeat.net/vbspeed/c_SwapEndian.htm

Don

 |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8 9 10 11
Prev: VB 6 & VS?
Next: C:\WINDOWS\system32\ieframe.dll\1