Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. 2 ques regarding hexadecimal numbers

2 ques regarding hexadecimal numbers

Scheduled Pinned Locked Moved Visual Basic
question
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    r i s h a b h s
    wrote on last edited by
    #1

    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

    Richard DeemingR 2 Replies Last reply
    0
    • R r i s h a b h s

      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

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2
      1. In VB/VB.NET, hexadecimal numbers are prefixed with &H, not 0x. 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, use i.ToString("X") in VB.NET, or Hex(i) in VB6.

      2. 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 = 1

        Do 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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      • R r i s h a b h s

        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

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        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**), not sum = 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 bitwise And 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

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        R 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          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**), not sum = 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 bitwise And 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

          R Offline
          R Offline
          r i s h a b h s
          wrote on last edited by
          #4

          thanx a lot :) :rose:

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups