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. Managed C++/CLI
  4. Reading a Float from memory

Reading a Float from memory

Scheduled Pinned Locked Moved Managed C++/CLI
data-structuresperformancehelpquestion
7 Posts 5 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.
  • M Offline
    M Offline
    Mattzimmerer
    wrote on last edited by
    #1

    Wow I am burning my brain right here... I'm so very close to just saying screw it and writing some sort of conversion myself. First I'd love to know if there is an existing macro to do what I want to do... I have a char array: char OutputBuffer[1024] = {0}; (huge i know, but ill lower the size later ;P) that has a byte put into it... (78 AB 45 44) is a value I had at one time... as a float it means 790.67919921875 note!: this value has been read straight from memory The problem is I cant get this DWORD into a float for the life of me... Is there any existing macro? If not throw me a bone and tell me how i might convert this into a float from scratch...

    L G L 3 Replies Last reply
    0
    • M Mattzimmerer

      Wow I am burning my brain right here... I'm so very close to just saying screw it and writing some sort of conversion myself. First I'd love to know if there is an existing macro to do what I want to do... I have a char array: char OutputBuffer[1024] = {0}; (huge i know, but ill lower the size later ;P) that has a byte put into it... (78 AB 45 44) is a value I had at one time... as a float it means 790.67919921875 note!: this value has been read straight from memory The problem is I cant get this DWORD into a float for the life of me... Is there any existing macro? If not throw me a bone and tell me how i might convert this into a float from scratch...

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

      Mattzimmerer wrote:

      The problem is I cant get this DWORD into a float for the life of me...

      If this is already a float (as you state) then there is nothing to do. If you mean reference it as a float then you just use the (float) cast in front of the reference. Or do I misunderstand your problem?

      MVP 2010 - are they mad?

      1 Reply Last reply
      0
      • M Mattzimmerer

        Wow I am burning my brain right here... I'm so very close to just saying screw it and writing some sort of conversion myself. First I'd love to know if there is an existing macro to do what I want to do... I have a char array: char OutputBuffer[1024] = {0}; (huge i know, but ill lower the size later ;P) that has a byte put into it... (78 AB 45 44) is a value I had at one time... as a float it means 790.67919921875 note!: this value has been read straight from memory The problem is I cant get this DWORD into a float for the life of me... Is there any existing macro? If not throw me a bone and tell me how i might convert this into a float from scratch...

        G Offline
        G Offline
        Graham Breach
        wrote on last edited by
        #3

        I don't know about an existing macro, but this will do it:

        char OutputBuffer[4] = { 0x78, 0xAB, 0x45, 0x44 };
        float d = *((float*)&OutputBuffer[0]);

        M 1 Reply Last reply
        0
        • M Mattzimmerer

          Wow I am burning my brain right here... I'm so very close to just saying screw it and writing some sort of conversion myself. First I'd love to know if there is an existing macro to do what I want to do... I have a char array: char OutputBuffer[1024] = {0}; (huge i know, but ill lower the size later ;P) that has a byte put into it... (78 AB 45 44) is a value I had at one time... as a float it means 790.67919921875 note!: this value has been read straight from memory The problem is I cant get this DWORD into a float for the life of me... Is there any existing macro? If not throw me a bone and tell me how i might convert this into a float from scratch...

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Assuming your code is managed and you're in the right forum, have a look at BitConverter.ToSingle() :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
          [The QA section does it automatically now, I hope we soon get it on regular forums as well]


          D 1 Reply Last reply
          0
          • G Graham Breach

            I don't know about an existing macro, but this will do it:

            char OutputBuffer[4] = { 0x78, 0xAB, 0x45, 0x44 };
            float d = *((float*)&OutputBuffer[0]);

            M Offline
            M Offline
            Mattzimmerer
            wrote on last edited by
            #5

            This worked great, thanks! Now i have a new trick lool, why didnt i think of that: cast it as a pointer of the type i know it is and then deference it =D...

            1 Reply Last reply
            0
            • L Luc Pattyn

              Assuming your code is managed and you're in the right forum, have a look at BitConverter.ToSingle() :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
              [The QA section does it automatically now, I hope we soon get it on regular forums as well]


              D Offline
              D Offline
              dybs
              wrote on last edited by
              #6

              One caveat to the BitConverter class is the endianness it uses is based on the architecture of the machine the program is running on. So if you're interfacing to a piece of hardware and its endianness doesn't match the endian-ness of the computer, or you're reading a binary file created on a computer with a different endianness and using BitConverter, then BitConverter will not give the correct result. Dybs

              The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen

              L 1 Reply Last reply
              0
              • D dybs

                One caveat to the BitConverter class is the endianness it uses is based on the architecture of the machine the program is running on. So if you're interfacing to a piece of hardware and its endianness doesn't match the endian-ness of the computer, or you're reading a binary file created on a computer with a different endianness and using BitConverter, then BitConverter will not give the correct result. Dybs

                The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Sure, whatever method is used, one better is aware of all applying conventions; endianness being the most relevant one here. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


                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