Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15006

Swap Word UDF error while handling negative values

$
0
0
The udf below outputs swapped long after WORD swap. It handles perfectly positives. How can it be modified to handle negative input? For es=xample, with n = +305419896 the output is 2018915346. But fails with n = -305419896.

\
Code:

Public Function SwapWords( _
      ByRef n As Long) As Long

    Const m As Integer = 3

    Dim b(m) As Byte, i&, u$, v$

    u = VBA.Right("00000000" + VBA.Hex(n), 2& * (m + 1&))
    v = vbNullString

    For i = 0& To m
        v = v + VBA.Mid(u, (m - i) * 2& + 1&, 2&)
        b(i) = VBA.Mid(u, (m - i) * 2& + 1&, 2&)
    Next i

    SwapWords = CLng("&H" + v)
    Exit Function

End Function


Viewing all articles
Browse latest Browse all 15006

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>