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. float to hex

float to hex

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

    Hi, Is there a method that will convert an float to a hex number? Thanks.

    D 1 Reply Last reply
    0
    • J Jay Hova

      Hi, Is there a method that will convert an float to a hex number? Thanks.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      A "hex" number is how it is represented, not how it is stored (i.e., its type). All numbers have a base-16 representation, be they floating-point or integer. For example, if I had a number (12345) and I wanted to see its base-16 representation, I'd use:

      int num = 12345;
      char szNum[16];
      sprintf(szNum, "%#x", num);

      szNum now contains "0x3039." No conversion was done, it's just shown in a different base. See if these two MSDN articles help you along any: Q42980 Q125056

      J 1 Reply Last reply
      0
      • D David Crow

        A "hex" number is how it is represented, not how it is stored (i.e., its type). All numbers have a base-16 representation, be they floating-point or integer. For example, if I had a number (12345) and I wanted to see its base-16 representation, I'd use:

        int num = 12345;
        char szNum[16];
        sprintf(szNum, "%#x", num);

        szNum now contains "0x3039." No conversion was done, it's just shown in a different base. See if these two MSDN articles help you along any: Q42980 Q125056

        J Offline
        J Offline
        Jay Hova
        wrote on last edited by
        #3

        Thanks for the help. Where can I find those articles, on the msdn.microsoft site? Just to clarify: I am sending information from my program to someone else via the internet. I was told that the information has to be sent in hex format. So are you saying i can just send a float number? Thanks for all your help

        D R 2 Replies Last reply
        0
        • J Jay Hova

          Thanks for the help. Where can I find those articles, on the msdn.microsoft site? Just to clarify: I am sending information from my program to someone else via the internet. I was told that the information has to be sent in hex format. So are you saying i can just send a float number? Thanks for all your help

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Jay Hova wrote: Where can I find those articles, on the msdn.microsoft site? Yes. Jay Hova wrote: Just to clarify: I am sending information from my program to someone else via the internet. I was told that the information has to be sent in hex format. So are you saying i can just send a float number? See this thread.

          R 1 Reply Last reply
          0
          • J Jay Hova

            Thanks for the help. Where can I find those articles, on the msdn.microsoft site? Just to clarify: I am sending information from my program to someone else via the internet. I was told that the information has to be sent in hex format. So are you saying i can just send a float number? Thanks for all your help

            R Offline
            R Offline
            Roger Wright
            wrote on last edited by
            #5

            I'm not sure what they're trying to say - "Hex format"? It could many several things. The IEEE format for representing a floating point number is S EEEEEEEE FFFFFFFFFFFFFFFFFFFFFFF 0 1 8 9 31 where S = the sign bit, EEEEEEEE = the exponent, and FFF...F = the fractional mantissa. In this format each bit has meaning, and on the receiving end the consumer has to expect and recognize this format. When you send it in hex, you're just sending bits in groups of 4, or SEEE EEEE EFFF FFFF FFFF FFFF FFFF FFFF Hex0 Hex1 Hex2 Hex3 Hex4 Hex5 Hex6 Hex7 It's up to the receiver to combine the 8 hex characters' bit pattern into a single 32-bit value and correctly interpret it as a floating point value. On the other hand, if your application requires you to send text data only, using the ascii characters that represent the hex characters that are equivalent to your floating point value, you have a real challenge. First you'll have to convert the float into a collection of 4-bit chunks, then determine the hex character for each chunk, then convert each hex character to text. I wouldn't know how to begin that, I'm afraid, though almost everyone here is smarter than I about such things and may be able to guide you. Hopefully you're dealing with the first case...

            "Welcome to Arizona!
            Drive Nice - We're Armed..."
            - Proposed Sign at CA/AZ Border

            1 Reply Last reply
            0
            • D David Crow

              Jay Hova wrote: Where can I find those articles, on the msdn.microsoft site? Yes. Jay Hova wrote: Just to clarify: I am sending information from my program to someone else via the internet. I was told that the information has to be sent in hex format. So are you saying i can just send a float number? See this thread.

              R Offline
              R Offline
              Roger Wright
              wrote on last edited by
              #6

              Great... While I'm off, straining my tiny brain to come up with something that might just pass for helpful, you pop in with an easy solution. Sheesh!:laugh:

              "Welcome to Arizona!
              Drive Nice - We're Armed..."
              - Proposed Sign at CA/AZ Border

              D 1 Reply Last reply
              0
              • R Roger Wright

                Great... While I'm off, straining my tiny brain to come up with something that might just pass for helpful, you pop in with an easy solution. Sheesh!:laugh:

                "Welcome to Arizona!
                Drive Nice - We're Armed..."
                - Proposed Sign at CA/AZ Border

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                I know the feeling. http://www.codeproject.com/script/comments/forums.asp?forumid=1647&select=592740#xx592740xx

                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