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. C / C++ / MFC
  4. Converting Char* hex to Integer..?

Converting Char* hex to Integer..?

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 4 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.
  • U Offline
    U Offline
    User 404410
    wrote on last edited by
    #1

    Hi~~ i have a string containing an hex number and i want to convert it to an integer. -------------------------------------------------------------- char *temp = "0x3e40"; i want to convert it to an interger.. which is 15936 how do i do that? Thanks

    RaviBeeR T 2 Replies Last reply
    0
    • U User 404410

      Hi~~ i have a string containing an hex number and i want to convert it to an integer. -------------------------------------------------------------- char *temp = "0x3e40"; i want to convert it to an interger.. which is 15936 how do i do that? Thanks

      RaviBeeR Offline
      RaviBeeR Offline
      RaviBee
      wrote on last edited by
      #2

      unsigned long nValue = 0;
      long nConverted = sscanf (temp, "%x", &nValue);
      if (nConverted != 1) {
      failure;
      }

      /ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com

      U 1 Reply Last reply
      0
      • RaviBeeR RaviBee

        unsigned long nValue = 0;
        long nConverted = sscanf (temp, "%x", &nValue);
        if (nConverted != 1) {
        failure;
        }

        /ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com

        U Offline
        U Offline
        User 404410
        wrote on last edited by
        #3

        Thanks :):):)

        1 Reply Last reply
        0
        • U User 404410

          Hi~~ i have a string containing an hex number and i want to convert it to an integer. -------------------------------------------------------------- char *temp = "0x3e40"; i want to convert it to an interger.. which is 15936 how do i do that? Thanks

          T Offline
          T Offline
          Toni78
          wrote on last edited by
          #4

          you could also use: char *temp = "0x3e40\0"; int iNum = atoi( temp ) // Afterall, I realized that even my comment lines have bugs When one cannot invent, one must at least improve (in bed).-My latest fortune cookie

          J 1 Reply Last reply
          0
          • T Toni78

            you could also use: char *temp = "0x3e40\0"; int iNum = atoi( temp ) // Afterall, I realized that even my comment lines have bugs When one cannot invent, one must at least improve (in bed).-My latest fortune cookie

            J Offline
            J Offline
            Johnny
            wrote on last edited by
            #5

            Although atoi only converts base 10 numbers - strtol (temp, NULL, 16) would be correct for hex!

            T 1 Reply Last reply
            0
            • J Johnny

              Although atoi only converts base 10 numbers - strtol (temp, NULL, 16) would be correct for hex!

              T Offline
              T Offline
              Toni78
              wrote on last edited by
              #6

              Johnny ² wrote: Although atoi only converts base 10 numbers Thank you for pointing that out. I completely forgot about the fact that atoi works only with base 10 numbers. I am sorry for posting a message without testing the code first. // Afterall, I realized that even my comment lines have bugs When one cannot invent, one must at least improve (in bed).-My latest fortune cookie

              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