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. Managed C++/CLI
  4. Convert string to integer only such that if it is alphanumeric or float it shouldnot return anything

Convert string to integer only such that if it is alphanumeric or float it shouldnot return anything

Scheduled Pinned Locked Moved Managed C++/CLI
json
3 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.
  • A Offline
    A Offline
    Abinash Mohanty
    wrote on last edited by
    #1

    Hi All, I want to covert a string to integer.Consider the string as char str[10]; and integer as int num; My case is as following:- 1) Suppose str = "10" num should be = 10 2) Suppose str = "10.88" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. 3) Suppose str = "10ab" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. Is there any direct API by which we can identify the above cases. I have tried atoi() but it returns the remaining integer value after removing the float or alphanumeric values. Or I have to try it by creating my own method.

    J 1 Reply Last reply
    0
    • A Abinash Mohanty

      Hi All, I want to covert a string to integer.Consider the string as char str[10]; and integer as int num; My case is as following:- 1) Suppose str = "10" num should be = 10 2) Suppose str = "10.88" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. 3) Suppose str = "10ab" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. Is there any direct API by which we can identify the above cases. I have tried atoi() but it returns the remaining integer value after removing the float or alphanumeric values. Or I have to try it by creating my own method.

      J Offline
      J Offline
      John Schroedl
      wrote on last edited by
      #2

      Since this is the C++/CLI forum, I'll give you a Managed reply -- use the TryParse static method on Int32 and/or Double classes.

      int val;
      String^ s = "10.22";
      if (System::Int32::TryParse(s,val))
      // parsed as Int32.
      else
      // try something else...

      John

      A 1 Reply Last reply
      0
      • J John Schroedl

        Since this is the C++/CLI forum, I'll give you a Managed reply -- use the TryParse static method on Int32 and/or Double classes.

        int val;
        String^ s = "10.22";
        if (System::Int32::TryParse(s,val))
        // parsed as Int32.
        else
        // try something else...

        John

        A Offline
        A Offline
        Abinash Mohanty
        wrote on last edited by
        #3

        Thanks John for your reply. I am sorry I posted this thread in C++/CLI forum. Actually I was looking for a solution in C++/MFC. Sorry for the inconvinience. Cheers, Abinash

        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