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. Convert string to long

Convert string to long

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

    Hi, How can I convert a string to long, so that the long value will be exactly the same value as the string? When I use the following code: CString str= "39436573472344"; long temp = atol(str); The value of temp is 2147483647. How can I get the value of temp to be the same as the string? Thanks.

    C M 2 Replies Last reply
    0
    • S SWDevil

      Hi, How can I convert a string to long, so that the long value will be exactly the same value as the string? When I use the following code: CString str= "39436573472344"; long temp = atol(str); The value of temp is 2147483647. How can I get the value of temp to be the same as the string? Thanks.

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      The numeric value corrensponding to your string simply doesn't fit inside a variable of type long; in such cases the atol returns LONG_MAX (i.e. 2147483647). You can use _strtoi64 function and a variable of type __int64 that holds values up to 9223372036854775807. :)

      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.
      [my articles]

      1 Reply Last reply
      0
      • S SWDevil

        Hi, How can I convert a string to long, so that the long value will be exactly the same value as the string? When I use the following code: CString str= "39436573472344"; long temp = atol(str); The value of temp is 2147483647. How can I get the value of temp to be the same as the string? Thanks.

        M Offline
        M Offline
        Matthew Faithfull
        wrote on last edited by
        #3

        Your problem here is that the number is too large to be stored in 32 bit signed long. You'll need a 64 bit integer type for this one, _int64 _atoi64( const char *string ); should do the trick or its UNICODE equivalent.

        Nothing is exactly what it seems but everything with seems can be unpicked.

        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