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. Casting a char array to a double

Casting a char array to a double

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelpquestion
7 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.
  • K Offline
    K Offline
    kyledunn
    wrote on last edited by
    #1

    I have a char array defined as char cBuffer[200] and I want to convert this to a double. Can anyone help with the syntax to do this? Thanks, Kyle

    L M N 3 Replies Last reply
    0
    • K kyledunn

      I have a char array defined as char cBuffer[200] and I want to convert this to a double. Can anyone help with the syntax to do this? Thanks, Kyle

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

      use: double dblVal; sscanf(cBuffer, "%lf", &dblVal);

      1 Reply Last reply
      0
      • K kyledunn

        I have a char array defined as char cBuffer[200] and I want to convert this to a double. Can anyone help with the syntax to do this? Thanks, Kyle

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

        If you mean you have a double in the first 8 bytes of the array then you can get at it like this: double dbl = *((double*)cBuffer); or alternatively if the bytes that contain the double start say 6 bytes into the array then the syntax is nearly the same but you need the address of the first byte of the double: double dbl = *((double*)&cBuffer[5]); Just one thing to be careful of is that x86 cpu's allow this casting from anywhere in memory but some cpu's have strict rules about things being aligned on even byte boundaries so if portability is important then you may need to copy the bytes using memcpy() or something first to ensure the proper alignment. Matt

        1 Reply Last reply
        0
        • K kyledunn

          I have a char array defined as char cBuffer[200] and I want to convert this to a double. Can anyone help with the syntax to do this? Thanks, Kyle

          N Offline
          N Offline
          Nish Nishant
          wrote on last edited by
          #4

          Casting it to double? Or converting the contents to double? Nish


          Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.

          K 1 Reply Last reply
          0
          • N Nish Nishant

            Casting it to double? Or converting the contents to double? Nish


            Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.

            K Offline
            K Offline
            kyledunn
            wrote on last edited by
            #5

            Converting the contents to double describes it well. Kyle

            N 1 Reply Last reply
            0
            • K kyledunn

              Converting the contents to double describes it well. Kyle

              N Offline
              N Offline
              Nish Nishant
              wrote on last edited by
              #6

              kyledunn wrote: Converting the contents to double describes it well. Then the solution is elementary. Use atof(...)

              double num = atof(buffer);

              Hope that helped, Nish


              Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.

              K 1 Reply Last reply
              0
              • N Nish Nishant

                kyledunn wrote: Converting the contents to double describes it well. Then the solution is elementary. Use atof(...)

                double num = atof(buffer);

                Hope that helped, Nish


                Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.

                K Offline
                K Offline
                kyledunn
                wrote on last edited by
                #7

                Yes, that did help. Thanks. Kyle

                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