vb.net convert hex to decimal - Beware of overflow

First thing to do is to understand hex or hexadecimal.

To understand hex, the first rule is to know that it is a numbering system and hence it consists of a series of number represented by 0-9, A-F.

Hence, it starts from 00,01, 0F,10... FF

So, what happens after FF ?

The answer is, since FF is equivalent to 255, what comes next is 256.

Using scientific calculator, 256 (decimal)--> 0100 (hex).

For example, manual conversion of a hex number from B372837E to decimal representation, using MS Excel can be calculated as:

= [11*POWER(16,7)] + [3 * POWER(16,6)] + [7 * POWER(16,5)] + [2 * POWER(16,4)] + [8 * POWER(16,3)] + [3 * POWER(16,2)] + [7 * POWER(16,1)] + [14 * POWER(16,0)]

In terms of programming in VB.NET or C#.NET, to convert a hex to decimal, just need to use 'System.Convert.ToInt32' function.

However, please take note that the tricky part is always about overflow.

In retrospect, the decimal equivalent of B372837E is 3010626430 or 3,010,626,430.

Hence, if you perform System.Convert.ToInt32(tempStrHex, 16), it will result in a funny negative number, this is because Int32 is actually a signed integer; the maximum positive number is 2,147,483,647.

The solution is to make use of 64 bits integer.

Dim tempNum as UInt64

tempNum = System.Convert.ToInt64("B372837E", 16)

It will work.

Comments