How to convert Hexadecimal number to decimal number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sivadhanekula
    New Member
    • Nov 2008
    • 58

    How to convert Hexadecimal number to decimal number

    Hi everyone...

    I have a data with both decimal and Hexadecimal numbers. I am extracting the required data from for analysis with the help of MINITAB 15. The problem is MINITAB 15 is not accepting the Hexdec data, so while extracting itself I need to convert the data from Hex to decimal. For extracting the data I am using Visual Studios "VC++" can anyone help me how to do the conversion.

    Thanks in advance

    Regards
    Siva
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You will need to write a function that can convert a string (from your file) into an integer where the string has a hexadecimal number.

    The conversion is straightforward . The hex number 1AB is just 1 * 256 + 10 * 16 + 11 * 1. 256 is 16 squared.

    Your function will have a loop and you will start at the first character of the string. This is the leftmost or most significant digit of the hex number. You will need to count the characters in the value to get the correct power of 16 for this leftmost digit. Then you convert the character to a hex integer. Then multiply by the power of 16 you just calculated. Add this to your total. Next, move to the second characterof the string and divide your power of 16 by 16 and repeat the conversion steps. When you get ot the end of the string, your are done. Finally, return your total as an unsigned interger (hex numbers cannot be negative).

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Take a look at the man pages for standard library functions strtol and strtoul.

      Comment

      • sivadhanekula
        New Member
        • Nov 2008
        • 58

        #4
        You will need to write a function that can convert a string (from your file) into an integer where the string has a hexadecimal number.

        The conversion is straightforward . The hex number 1AB is just 1 * 256 + 10 * 16 + 11 * 1. 256 is 16 squared.

        Your function will have a loop and you will start at the first character of the string. This is the leftmost or most significant digit of the hex number. You will need to count the characters in the value to get the correct power of 16 for this leftmost digit. Then you convert the character to a hex integer. Then multiply by the power of 16 you just calculated. Add this to your total. Next, move to the second characterof the string and divide your power of 16 by 16 and repeat the conversion steps. When you get ot the end of the string, your are done. Finally, return your total as an unsigned interger (hex numbers cannot be negative).
        Thank you for the reply. To be frank I am not that much good at programming, I understood each and every line of your reply but not able to write code for that. I have tried but got stuck at "how to call individual string values and how to multiply with the reducing power of 16"..I will be very much helpfull if you help me regarding this

        Thank you

        Regards
        Siva

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          If you're using C have a look at the %i or %x format specifiers; if you're using C++ have a look at the hex manipulator.

          kind regards,

          Jos

          Comment

          • Andr3w
            New Member
            • Nov 2007
            • 42

            #6
            Hey mate,

            You can check also this link http://en.wikipedia.org/wiki/Hexadecimal which besides historical information gives you information on how to convert hex values to other systems ( not only decimal ) and the opposite.

            Hope this helps

            Comment

            • whodgson
              Contributor
              • Jan 2007
              • 542

              #7
              Another way of explaining it from:
              (Programming with C++ John R Hubbard) sets out the algorithm for
              h[k].......h[2]h[1]h[0]:
              1. Set x = 0.
              2, Set j = k+1 (actual number of bits in the hex string)
              3. Subtract 1 from j.
              4. Multiply x by 16.
              5. Add h[j] to x.
              6. If j>0,repeat steps 3 - 6.
              7. Return x.
              example: convert f4d9 to decimal.
              --------------------------------------------------------
              j ....... h[j] .............x= 2.x + h[j]
              ---------------------------------------------------------
              4 ............... ............... ..............0
              3 ......... f ............... 16.0+ f = 15 ......[16.0 ==16 times 0]
              2 ......... 4 ............16. 15+4 = 244
              1 ......... d .......16.244+1 3 = 3917
              0 ......... 9 .......16.3917 + 9 = 62,681
              so f4d9 (base 16) = 62681 (base 10)
              Hope this helps

              Comment

              • whodgson
                Contributor
                • Jan 2007
                • 542

                #8
                The formula shown for x in the last thread is wrong.
                Instead of x=2.x +h[j] it should have been x=16.x + h[j].
                The trace of the algorithm is still correct.
                Hope no injuries have resulted.

                Comment

                • sivadhanekula
                  New Member
                  • Nov 2008
                  • 58

                  #9
                  ya there was no injuries and no wounds...itz perfect..
                  Thanks a lot for everyone!

                  c u all in other/next thread...ciao

                  Comment

                  Working...