Valhalla Legends Forums Archive | Visual Basic Programming | VB unsigned data types?

AuthorMessageTime
FuZe
Does visual basic support unsigned data types and if so, how do you declare it. Thx
August 2, 2003, 8:10 PM
Grok
Hmm ... yes.

[code]Dim b1 As Byte 'A data type used to hold positive integer numbers ranging from 0–255. Byte variables are stored as single, unsigned 8-bit (1-byte) numbers.[/code]

afaik, that's the only one.
August 2, 2003, 9:15 PM
DarkMinion
You can use Integer as unsigned long ;)
August 2, 2003, 10:01 PM
Yoni
[quote author=DarkMinion link=board=5;threadid=2183;start=0#msg16874 date=1059861697]
You can use Integer as unsigned long ;)
[/quote]
Hmm... More like Long as unsigned short, I think
August 3, 2003, 5:40 AM
Grok
Double is often used for unsigned long, as it has the significant digits to hold an unsigned long. Here are some helper functions.

[code]
Public Const OFFSET_4 = 4294967296#
Public Const MAXINT_4 = 2147483647
Public Const OFFSET_2 = 65536
Public Const MAXINT_2 = 32767

Public Function UnsignedToLong(Value As Double) As Long
'
'The function takes a Double containing a value in the
'range of an unsigned Long and returns a Long that you
'can pass to an API that requires an unsigned Long
'
If Value < 0 Or Value >= OFFSET_4 Then Error 6 ' Overflow
'
If Value <= MAXINT_4 Then
UnsignedToLong = Value
Else
UnsignedToLong = Value - OFFSET_4
End If
'
End Function

Public Function LongToUnsigned(Value As Long) As Double
'
'The function takes an unsigned Long from an API and
'converts it to a Double for display or arithmetic purposes
'
If Value < 0 Then
LongToUnsigned = Value + OFFSET_4
Else
LongToUnsigned = Value
End If
'
End Function

Public Function UnsignedToInteger(Value As Long) As Integer
'
'The function takes a Long containing a value in the range
'of an unsigned Integer and returns an Integer that you
'can pass to an API that requires an unsigned Integer
'
If Value < 0 Or Value >= OFFSET_2 Then Error 6 ' Overflow
'
If Value <= MAXINT_2 Then
UnsignedToInteger = Value
Else
UnsignedToInteger = Value - OFFSET_2
End If
'
End Function

Public Function IntegerToUnsigned(Value As Integer) As Long
'
'The function takes an unsigned Integer from an API and
'converts it to a Long for display or arithmetic purposes
'
If Value < 0 Then
IntegerToUnsigned = Value + OFFSET_2
Else
IntegerToUnsigned = Value
End If
'
End Function

[/code]
August 3, 2003, 3:42 PM
FuZe
The reason i asked this was because in the algorithm that encrypts the bnls password with the servercode, it accepts a long. When I recieve data, it is 4 bytes long, but there is an overflow error if the first digit of the hexadecimal is 8 or greater. So what I did was if the number was greater than the maximum long value (2^32)/2, i'd subtract it by 2^34 to get the negative long number.. and then sent it to BNLSchecksum. is this what your supposed to do? cuz i always get kicked off of bnls after sending the data....
August 4, 2003, 12:14 AM
drivehappy
See if you can declare your long variable as a double. I think I've had this problem before and it was because (I think) C++ version of a Long and VB's is different. Then again, I could be wrong but it's worth a shot.
August 4, 2003, 3:41 AM
Adron
[quote author=FuZe- link=board=5;threadid=2183;start=0#msg16946 date=1059956080]
So what I did was if the number was greater than the maximum long value (2^32)/2, i'd subtract it by 2^34 to get the negative long number.. [/quote]

You're supposed to subtract it by 2^32 ... But other than that your idea sounds right.
August 9, 2003, 5:17 PM
FuZe
[quote author=Adron link=board=5;threadid=2183;start=0#msg17557 date=1060449425]
[quote author=FuZe- link=board=5;threadid=2183;start=0#msg16946 date=1059956080]
So what I did was if the number was greater than the maximum long value (2^32)/2, i'd subtract it by 2^34 to get the negative long number.. [/quote]

You're supposed to subtract it by 2^32 ... But other than that your idea sounds right.
[/quote]

haha whoops typo ... i dunno how i put down 34.. ;\
but yea it did work. :)
August 10, 2003, 1:03 AM
Camel
[code]Public Function Add(ByVal number1 As Long, ByVal number2 As Long) As Long
Add = DtoL(CDbl(number1) + CDbl(number2))
End Function

Public Function DtoL(ByVal num As Double) As Long
While num > &H7FFFFFFF '2147483647
num = num - 4294967296#
Wend
While num < &H80000000 '-2147483648#
num = num + 4294967296#
Wend
DtoL = CLng(num)
End Function[/code]
I use this for Broken SHA-1 hashing to avoid the overflows. It seems pointless that VB even checks for overflows -- many of the people it's targeted at dont even understand what an overflow is.
August 10, 2003, 9:11 PM
Eibro
[quote author=Camel link=board=5;threadid=2183;start=0#msg17694 date=1060549897]
[code]Public Function Add(ByVal number1 As Long, ByVal number2 As Long) As Long
Add = DtoL(CDbl(number1) + CDbl(number2))
End Function

Public Function DtoL(ByVal num As Double) As Long
While num > &H7FFFFFFF '2147483647
num = num - 4294967296#
Wend
While num < &H80000000 '-2147483648#
num = num + 4294967296#
Wend
DtoL = CLng(num)
End Function[/code]
I use this for Broken SHA-1 hashing to avoid the overflows. It seems pointless that VB even checks for overflows -- many of the people it's targeted at dont even understand what an overflow is.
[/quote]Is it not possible to just disable them altogether? I doubt a very high percentage of bugs originate from integer overflow anyway. It's pretty clear (at least in my experience) when an integer i'm using has potential to overflow. With that, often times you'll see code which relies on the knowledge that an integer will overflow at a given point.
August 10, 2003, 9:30 PM
kamakazie
[quote author=Eibro link=board=5;threadid=2183;start=0#msg17698 date=1060551057]
Is it not possible to just disable them altogether? I doubt a very high percentage of bugs originate from integer overflow anyway. It's pretty clear (at least in my experience) when an integer i'm using has potential to overflow. With that, often times you'll see code which relies on the knowledge that an integer will overflow at a given point.
[/quote]

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconremoveintegeroverflowchecks.asp

Perhaps?
August 10, 2003, 9:39 PM
Camel
[quote author=kamakazie link=board=5;threadid=2183;start=0#msg17700 date=1060551579]
[quote author=Eibro link=board=5;threadid=2183;start=0#msg17698 date=1060551057]
Is it not possible to just disable them altogether? I doubt a very high percentage of bugs originate from integer overflow anyway. It's pretty clear (at least in my experience) when an integer i'm using has potential to overflow. With that, often times you'll see code which relies on the knowledge that an integer will overflow at a given point.
[/quote]

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconremoveintegeroverflowchecks.asp

Perhaps?
[/quote]

The app still crashes, it just doesnt bitch.
August 11, 2003, 1:12 AM
Adron
No, the application runs fine. But it only works in compiled code.
August 11, 2003, 8:49 AM
Camel
[quote author=Adron link=board=5;threadid=2183;start=0#msg17775 date=1060591779]
No, the application runs fine. But it only works in compiled code.
[/quote]

Hrmph; I could have sworn the program crashed last time I tried it. Oh well.
August 11, 2003, 5:28 PM
Adron
Try a form with two text boxes, two labels, and this code:

[code]
Option Explicit

Private Sub Text1_Change()
On Error GoTo adderr
Label1 = CLng(Text1) + CLng(Text2)
domul:
On Error GoTo mulerr
Label2 = CLng(Text1) * CLng(Text2)
Exit Sub
adderr:
Label1 = "Error"
Resume domul
mulerr:
Label2 = "Error"
End Sub

Private Sub Text2_Change()
On Error GoTo adderr
Label1 = CLng(Text1) + CLng(Text2)
domul:
On Error GoTo mulerr
Label2 = CLng(Text1) * CLng(Text2)
Exit Sub
adderr:
Label1 = "Error"
Resume domul
mulerr:
Label2 = "Error"
End Sub
[/code]
August 11, 2003, 6:12 PM

Search