Exception on arithmetic operation
Why does this bit of code throw an exception?
dim dataBytes(5) as byte
dataBytes(0)=&H4A
dataBytes(1)=&H1
dataBytes(2)=&H1
dataBytes(3)=&H2A
dataBytes(4)=(dataBytes(1)+dataBytes(2)+dataBytes(3))^&HFF
The final statement is what generates the exception. The exception states that an arithmetic operation resulted in an overflow.
Thanks!
Re: Exception on arithmetic operation
To answer my own question...
I mixed C syntax with VB.net syntax. ^ is an XOR operation in C... in VB.net it's simply Xor. ^ is obviously valid in VB.net and so it didn't generate any errors. Dumb mistake... but I guess it's bound to happen when using both languages simultaneously.
Re: Exception on arithmetic operation
byte type represent 8-bit unsigned integers ranges b/w 0-255. the arithematic operation cause an overflow.
in vb.net ^ operator to raise a number to the power of an exponent.
so use the xor operator.
Code:
dataBytes(4) = (dataBytes(1) + dataBytes(2) + dataBytes(3)) Xor &HFF