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. float from bytes

float from bytes

Scheduled Pinned Locked Moved C#
questioncsharpdata-structures
5 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.
  • _ Offline
    _ Offline
    __John_
    wrote on last edited by
    #1

    Hi, I need to convert 4 bytes in to a float or double in C#. The 4 bytes come from the comm port and are read in to a byte array, how can I convert back to a float?

    “If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford

    OriginalGriffO A 2 Replies Last reply
    0
    • _ __John_

      Hi, I need to convert 4 bytes in to a float or double in C#. The 4 bytes come from the comm port and are read in to a byte array, how can I convert back to a float?

      “If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Try BitConverter.ToSingle[^] - can't guarantee it will work, it depends on the originator floating point format. If it give ridiculous values, try swapping the byte order: ABCD -> DCBA Then try it again.

      Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • _ __John_

        Hi, I need to convert 4 bytes in to a float or double in C#. The 4 bytes come from the comm port and are read in to a byte array, how can I convert back to a float?

        “If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford

        A Offline
        A Offline
        Amarnath S
        wrote on last edited by
        #3

        You could also try:

        int res = 0;

        if (littleEndian)
        {
        res += b[0];
        res += (((int)b[1]) << 8);
        res += (((int)b[2]) << 16);
        res += (((int)b[3]) << 24);
        }
        else
        {
        res += b[3];
        res += (((int)b[2]) << 8);
        res += (((int)b[1]) << 16);
        res += (((int)b[0]) << 24);
        }

        float f;
        f = Convert.ToSingle(res);

        Of course, littleendian needs to be defined and assigned earlier. Converting to double is similar - you need to replace int by long and use Convert.ToDouble(res).

        _ B 2 Replies Last reply
        0
        • A Amarnath S

          You could also try:

          int res = 0;

          if (littleEndian)
          {
          res += b[0];
          res += (((int)b[1]) << 8);
          res += (((int)b[2]) << 16);
          res += (((int)b[3]) << 24);
          }
          else
          {
          res += b[3];
          res += (((int)b[2]) << 8);
          res += (((int)b[1]) << 16);
          res += (((int)b[0]) << 24);
          }

          float f;
          f = Convert.ToSingle(res);

          Of course, littleendian needs to be defined and assigned earlier. Converting to double is similar - you need to replace int by long and use Convert.ToDouble(res).

          _ Offline
          _ Offline
          __John_
          wrote on last edited by
          #4

          Thanks for the replies. I will give it a try later today.

          “If I had asked people what they wanted, they would have said faster horses.” ― Henry Ford

          1 Reply Last reply
          0
          • A Amarnath S

            You could also try:

            int res = 0;

            if (littleEndian)
            {
            res += b[0];
            res += (((int)b[1]) << 8);
            res += (((int)b[2]) << 16);
            res += (((int)b[3]) << 24);
            }
            else
            {
            res += b[3];
            res += (((int)b[2]) << 8);
            res += (((int)b[1]) << 16);
            res += (((int)b[0]) << 24);
            }

            float f;
            f = Convert.ToSingle(res);

            Of course, littleendian needs to be defined and assigned earlier. Converting to double is similar - you need to replace int by long and use Convert.ToDouble(res).

            B Offline
            B Offline
            BobJanova
            wrote on last edited by
            #5

            This will make the floating point equivalent of the 4 byte integer interpretation of the bytes (i.e. "08 01 00 00" will make you 264.0), not the IEEE single precision floating point number represented there.

            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