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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Function for converting HEX to DEC?

Function for converting HEX to DEC?

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 Posts 5 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.
  • M Offline
    M Offline
    m_mun
    wrote on last edited by
    #1

    Hallo all, Is there any single function, which convert hex number to decimal number. I will pass just only two characters like "3D" as a string. Thanks all Regards Shaheen

    L L CPalliniC 3 Replies Last reply
    0
    • M m_mun

      Hallo all, Is there any single function, which convert hex number to decimal number. I will pass just only two characters like "3D" as a string. Thanks all Regards Shaheen

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      strtoul ANSI C/C++ function is possible answer of your question. :) [EDIT] Broken link

      Life is a stage and we are all actors!

      modified on Wednesday, August 26, 2009 7:26 AM

      M 2 Replies Last reply
      0
      • L Lost User

        strtoul ANSI C/C++ function is possible answer of your question. :) [EDIT] Broken link

        Life is a stage and we are all actors!

        modified on Wednesday, August 26, 2009 7:26 AM

        M Offline
        M Offline
        m_mun
        wrote on last edited by
        #3

        Thank for for reply. I am just try it

        1 Reply Last reply
        0
        • L Lost User

          strtoul ANSI C/C++ function is possible answer of your question. :) [EDIT] Broken link

          Life is a stage and we are all actors!

          modified on Wednesday, August 26, 2009 7:26 AM

          M Offline
          M Offline
          m_mun
          wrote on last edited by
          #4

          Can u give me a example. I have try it but not works.

          L C 2 Replies Last reply
          0
          • M m_mun

            Can u give me a example. I have try it but not works.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Here is basic one: printf("%u",strtoul("5A", NULL, 16)); ... and the output is 90.

            Life is a stage and we are all actors!

            M 1 Reply Last reply
            0
            • M m_mun

              Can u give me a example. I have try it but not works.

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              There is an example on the page that was given to you. By the way, "not works" is not really a clear description of the problem, it could be anything...

              Cédric Moonen Software developer
              Charting control [v2.0] OpenGL game tutorial in C++

              1 Reply Last reply
              0
              • M m_mun

                Hallo all, Is there any single function, which convert hex number to decimal number. I will pass just only two characters like "3D" as a string. Thanks all Regards Shaheen

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Hi, IIRC the string scanning function sscanf can do that for you like so:

                int result;
                sscanf("3D", "%2x", &result);

                :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                1 Reply Last reply
                0
                • L Lost User

                  Here is basic one: printf("%u",strtoul("5A", NULL, 16)); ... and the output is 90.

                  Life is a stage and we are all actors!

                  M Offline
                  M Offline
                  m_mun
                  wrote on last edited by
                  #8

                  Thanks it works

                  1 Reply Last reply
                  0
                  • M m_mun

                    Hallo all, Is there any single function, which convert hex number to decimal number. I will pass just only two characters like "3D" as a string. Thanks all Regards Shaheen

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #9

                    const char s[] = "3D";
                    int i = (s[0]<='9' ? s[0]-'0': s[0]-'A'+10) << 4 | (s[1]<='9' ? s[1]-'0': s[1]-'A'+10);

                    :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    In testa che avete, signor di Ceprano?

                    L 1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      const char s[] = "3D";
                      int i = (s[0]<='9' ? s[0]-'0': s[0]-'A'+10) << 4 | (s[1]<='9' ? s[1]-'0': s[1]-'A'+10);

                      :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      now that will choke on lower-case hex numbers, won't it? :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                      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