Page 1 of 1

VB6 - Signed/Unsigned Integer Conversions [Archive] - ForumsHQ

Posted: Fri Sep 26, 2003 6:01 pm
by syntax53
Constants



Global Const LongOffset = 4294967296#

Global Const MaxLong = 2147483647

Global Const IntOffset = 65536

Global Const MaxInt = 32767



Unsigned Long to Signed Long



Public Function ULong2SLong(ByVal value As Variant) As Long

On Error GoTo error:

If value >= LongOffset Then value = LongOffset - 1

If value MaxLong Then value = MaxLong

If value = IntOffset Then value = IntOffset - 1

If value MaxInt Then value = MaxInt

If value < 0 Then

SInt2UInt = value + IntOffset

Else

SInt2UInt = value

End If

Exit Function

error:

HandleError

End Function



Error Handler



Public Sub HandleError()



MsgBox Err.Source & vbCrLf & "Error " & Err.Number & ": " & Err.Description, vbExclamation



Err.Clear

End Sub