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#
  4. IEEE 754 Converter

IEEE 754 Converter

Scheduled Pinned Locked Moved C#
helptutorialquestion
10 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.
  • J Offline
    J Offline
    JF2015
    wrote on last edited by
    #1

    Hi, i have a problem concerning float values. I receive a 4 Byte value through the serial port in the following order: SEEEEEEE EMMMMMMM MMMMMMMM MMMMMMMM S - Sign E - Exponent M - Mantissa so, for example i have: 43h 89h 80h 00h (h...hexadecimal) which is the decimal value of: 275.0 My question is, if anybody knows a way of converting the 4 Byte value to the float value. Thanks in advance for any help you can provide

    B G CPalliniC V 4 Replies Last reply
    0
    • J JF2015

      Hi, i have a problem concerning float values. I receive a 4 Byte value through the serial port in the following order: SEEEEEEE EMMMMMMM MMMMMMMM MMMMMMMM S - Sign E - Exponent M - Mantissa so, for example i have: 43h 89h 80h 00h (h...hexadecimal) which is the decimal value of: 275.0 My question is, if anybody knows a way of converting the 4 Byte value to the float value. Thanks in advance for any help you can provide

      B Offline
      B Offline
      blackjack2150
      wrote on last edited by
      #2

      In C++, you would write number = (sign ? -1:1) * 2^(exponent) * 1.(mantissa bits)

      CPalliniC 1 Reply Last reply
      0
      • J JF2015

        Hi, i have a problem concerning float values. I receive a 4 Byte value through the serial port in the following order: SEEEEEEE EMMMMMMM MMMMMMMM MMMMMMMM S - Sign E - Exponent M - Mantissa so, for example i have: 43h 89h 80h 00h (h...hexadecimal) which is the decimal value of: 275.0 My question is, if anybody knows a way of converting the 4 Byte value to the float value. Thanks in advance for any help you can provide

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        Use the BitConverter.ToSingle method.

        --- single minded; short sighted; long gone;

        J 1 Reply Last reply
        0
        • B blackjack2150

          In C++, you would write number = (sign ? -1:1) * 2^(exponent) * 1.(mantissa bits)

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Actually, with C++ you can be more extreme, for instance:

          unsigned char myBytes[4]={0x43, 0x89, 0x80, 0x00};
          unsigned char myReversedBytes[4];

          myReversedBytes[0] = myBytes[3];
          myReversedBytes[1] = myBytes[2];
          myReversedBytes[2] = myBytes[1];
          myReversedBytes[3] = myBytes[0];
          float f = *(float *)myReversedBytes;

          Note that if you're lucky the original bytes have not to be reversed. :-D

          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.

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • J JF2015

            Hi, i have a problem concerning float values. I receive a 4 Byte value through the serial port in the following order: SEEEEEEE EMMMMMMM MMMMMMMM MMMMMMMM S - Sign E - Exponent M - Mantissa so, for example i have: 43h 89h 80h 00h (h...hexadecimal) which is the decimal value of: 275.0 My question is, if anybody knows a way of converting the 4 Byte value to the float value. Thanks in advance for any help you can provide

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Note that, If you intend to use the Guffa solution (that indeed is the correct one), then you have to reverse the order of the bytes to get the correct result. :)

            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.

            In testa che avete, signor di Ceprano?

            J 1 Reply Last reply
            0
            • J JF2015

              Hi, i have a problem concerning float values. I receive a 4 Byte value through the serial port in the following order: SEEEEEEE EMMMMMMM MMMMMMMM MMMMMMMM S - Sign E - Exponent M - Mantissa so, for example i have: 43h 89h 80h 00h (h...hexadecimal) which is the decimal value of: 275.0 My question is, if anybody knows a way of converting the 4 Byte value to the float value. Thanks in advance for any help you can provide

              V Offline
              V Offline
              Vikram A Punathambekar
              wrote on last edited by
              #6

              Sounds like you need a union. :)

              Cheers, Vıkram.


              After all is said and done, much is said and little is done.

              CPalliniC 1 Reply Last reply
              0
              • G Guffa

                Use the BitConverter.ToSingle method.

                --- single minded; short sighted; long gone;

                J Offline
                J Offline
                JF2015
                wrote on last edited by
                #7

                Thanks a lot. This really helped me. I almost thought i would have to code it on my own.

                1 Reply Last reply
                0
                • CPalliniC CPallini

                  Note that, If you intend to use the Guffa solution (that indeed is the correct one), then you have to reverse the order of the bytes to get the correct result. :)

                  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.

                  J Offline
                  J Offline
                  JF2015
                  wrote on last edited by
                  #8

                  Thanks, you guys are a real good source for advice. No wonder that there are lots of people asking anyone of you instead of looking in the msdn documents.:-D

                  1 Reply Last reply
                  0
                  • V Vikram A Punathambekar

                    Sounds like you need a union. :)

                    Cheers, Vıkram.


                    After all is said and done, much is said and little is done.

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #9

                    Vikram A Punathambekar wrote:

                    Sounds like you need a union.

                    AFAIK C# doesn't provide it.:)

                    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.

                    In testa che avete, signor di Ceprano?

                    V 1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      Vikram A Punathambekar wrote:

                      Sounds like you need a union.

                      AFAIK C# doesn't provide it.:)

                      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.

                      V Offline
                      V Offline
                      Vikram A Punathambekar
                      wrote on last edited by
                      #10

                      [bangs head against wall]

                      Cheers, Vıkram.


                      After all is said and done, much is said and little is done.

                      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