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. any predefined function to convert string to int

any predefined function to convert string to int

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

    hello.... i have to read a file that has hex values.....i read them as string initially,i would want to know if there is already a function to convert this string(of hex) directly to int?....like we have for converting string(of int) to integer using atoi() fnction....?

    R S J 3 Replies Last reply
    0
    • N namaskaaram

      hello.... i have to read a file that has hex values.....i read them as string initially,i would want to know if there is already a function to convert this string(of hex) directly to int?....like we have for converting string(of int) to integer using atoi() fnction....?

      R Offline
      R Offline
      Ryan Binns
      wrote on last edited by
      #2

      sscanf() using the %x format specifier.

      Ryan

      "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

      N 1 Reply Last reply
      0
      • N namaskaaram

        hello.... i have to read a file that has hex values.....i read them as string initially,i would want to know if there is already a function to convert this string(of hex) directly to int?....like we have for converting string(of int) to integer using atoi() fnction....?

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        Here's one way to do it: ------------------------ #include #include using namespace std; int main(int argc, char* argv[]) { const char* Number = "baadf00d beeff00d"; istringstream ss; ss.str(Number); unsigned int num; while ( ss >> hex >> num ) { // Converted...Output the number in decimal. cout << num << endl; } return 0; } Steve

        1 Reply Last reply
        0
        • R Ryan Binns

          sscanf() using the %x format specifier.

          Ryan

          "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

          N Offline
          N Offline
          namaskaaram
          wrote on last edited by
          #4

          thank u ryan and stephen!..... :-)

          1 Reply Last reply
          0
          • N namaskaaram

            hello.... i have to read a file that has hex values.....i read them as string initially,i would want to know if there is already a function to convert this string(of hex) directly to int?....like we have for converting string(of int) to integer using atoi() fnction....?

            J Offline
            J Offline
            James R Twine
            wrote on last edited by
            #5

            You can use *scanf-type functions, but functions specifically designed to do convert strings to numeric values already exist and will likely perform better than the *scanf-type functions.  You do not need to use a shotgun to kill a fly... :)    Look up the strtol/wcstol, strtoul/wcstoul, and StrToIntEx functions.    The StrToIntEx function handles leading whitespace, and sign indicator (ignored), and a leading 0x hex. specifier.  Much more robust parsing than you get doing something simple like using "%x" with a *scanf-type function.    Peace! -=- James


            If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
            Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
            DeleteFXPFiles & CheckFavorites (Please rate this post!)

            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