2 ques regarding hexadecimal numbers
-
dear all, 1)how do i add hexadecimal numbers in vb (result too in hexadecimal numbers) 2) if there are 4 hexadecimal numbers say 0x40 0x80 0x1000 0x2000 and i have a number 0x10C2 (which is the hex sum of the top 3 numbers). now using such a number (which is the sum of any of the above hex numbers ) can i find which of the above numbers are included in the sum if it is ensured that a number is added only once. thnx in advance
-
dear all, 1)how do i add hexadecimal numbers in vb (result too in hexadecimal numbers) 2) if there are 4 hexadecimal numbers say 0x40 0x80 0x1000 0x2000 and i have a number 0x10C2 (which is the hex sum of the top 3 numbers). now using such a number (which is the sum of any of the above hex numbers ) can i find which of the above numbers are included in the sum if it is ensured that a number is added only once. thnx in advance
-
In VB/VB.NET, hexadecimal numbers are prefixed with
&H
, not0x
. You use hex numbers in exactly the same way as decimal numbers, e.g.Dim i As Integer = &H40 + &H80
. To print the result as a hex number, usei.ToString("X")
in VB.NET, orHex(i)
in VB6. -
The sum of the first three numbers is 10C0, not 10C2. You can use the
And
operator to find the numbers. For example:Dim number As Integer = &H10C0
Dim sum As String
Dim i As Integer
i = 1Do While number > 0
If 0 <> (number And i) Then
If Len(sum) > 0 Then sum = sum & " + "
sum = sum & Hex(i)
number = number - i
End If
i = i * 2
Loop
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
-
dear all, 1)how do i add hexadecimal numbers in vb (result too in hexadecimal numbers) 2) if there are 4 hexadecimal numbers say 0x40 0x80 0x1000 0x2000 and i have a number 0x10C2 (which is the hex sum of the top 3 numbers). now using such a number (which is the sum of any of the above hex numbers ) can i find which of the above numbers are included in the sum if it is ensured that a number is added only once. thnx in advance
rishabhs wrote: thanx for ur reply Richard! [rose] but the code u gave me doesn't returns all the numbers which r added to get the number &H10C0 plz. help me understand the logic of this job I fixed a typo in my previous post, which should give you the correct result. [You need
sum = sum + Hex(**i**)
, notsum = sum + Hex(number)
. :-O] The code walks through each binary bit of the number, and tests to see if that bit is set, using the bitwiseAnd
operator. [0 <> (number And i)
]. If the bit is set, it appends the hex representation of the bit to a string.0x10C0 = 0001 0000 1100 0000
= 0000 0000 0100 0000 + 0000 0000 1000 0000 + 0001 0000 0000 0000 = 0x0040 + 0x0080 + 0x1000
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
rishabhs wrote: thanx for ur reply Richard! [rose] but the code u gave me doesn't returns all the numbers which r added to get the number &H10C0 plz. help me understand the logic of this job I fixed a typo in my previous post, which should give you the correct result. [You need
sum = sum + Hex(**i**)
, notsum = sum + Hex(number)
. :-O] The code walks through each binary bit of the number, and tests to see if that bit is set, using the bitwiseAnd
operator. [0 <> (number And i)
]. If the bit is set, it appends the hex representation of the bit to a string.0x10C0 = 0001 0000 1100 0000
= 0000 0000 0100 0000 + 0000 0000 1000 0000 + 0001 0000 0000 0000 = 0x0040 + 0x0080 + 0x1000
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
thanx a lot :) :rose: