• Welcome to Valhalla Legends Archive.
 

VB unsigned data types?

Started by FuZe, August 02, 2003, 03:10 PM

Previous topic - Next topic

FuZe

Does visual basic support unsigned data types and if so, how do you declare it.  Thx

Grok

Hmm ... yes.

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.

afaik, that's the only one.

DarkMinion

You can use Integer as unsigned long  ;)

Yoni

Quote from: DarkMinion on August 02, 2003, 05:01 PM
You can use Integer as unsigned long  ;)
Hmm... More like Long as unsigned short, I think

Grok

Double is often used for unsigned long, as it has the significant digits to hold an unsigned long.  Here are some helper functions.


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



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....

drivehappy

#6
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.

Adron

Quote from: FuZe- on August 03, 2003, 07:14 PM
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..  

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

FuZe

Quote from: Adron on August 09, 2003, 12:17 PM
Quote from: FuZe- on August 03, 2003, 07:14 PM
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..  

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

haha whoops typo ... i dunno how i put down 34.. ;\
but yea it did work. :)

Camel

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

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.

Eibro

Quote from: Camel on August 10, 2003, 04:11 PM
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

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.
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.
Eibro of Yeti Lovers.

dxoigmn

Quote from: Eibro on August 10, 2003, 04:30 PM
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.

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

Perhaps?

Camel

Quote from: kamakazie on August 10, 2003, 04:39 PM
Quote from: Eibro on August 10, 2003, 04:30 PM
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.

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

Perhaps?

The app still crashes, it just doesnt bitch.

Adron

No, the application runs fine. But it only works in compiled code.

Camel

Quote from: Adron on August 11, 2003, 03:49 AM
No, the application runs fine. But it only works in compiled code.

Hrmph; I could have sworn the program crashed last time I tried it. Oh well.