Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unsigned to Long converter 1

Status
Not open for further replies.

elquixiote

Technical User
Nov 30, 2002
99
MX
Hi!. I'm reading a binary file that was created in a VMS plattform (probably in C++ or something alike). This file contains fields that were declared as unsigned integer, so, at the time I try to read these fields as integers I got negative numbers. For example, i got -14044 and the actual value of the field is about 52000. Is there a way to convert this unsigned integer value read into an integer variable to its real value in a long integer value ?

Thanks, ...

elQuixiote
 
Try this;
___
[tt]
Private Sub Form_Load()
MsgBox SignedToUnSingned(-14044)
End Sub

Function SignedToUnSingned(SignedInt As Integer) As Long
SignedToUnSingned = CLng("&H" & Hex$(SignedInt))
End Function[/tt]
 
And this is even better.
___
[tt]
Function SignedToUnSingned(SignedInt As Integer) As Long
If SignedInt < 0 Then SignedToUnSingned = 65536
SignedToUnSingned = SignedToUnSingned + SignedInt
End Function
[/tt]
 
Thank you...

Maybe you want to write a faq. I think could be useful to many persons ...

Thanks again...

elQuixiote ...
 
Even easier:
Unsigned = (Signed + 65536) Mod 65536

Note all these solutions only work for standard 2-byte integers

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top