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. how to store float number (4 bytes) to a byte array

how to store float number (4 bytes) to a byte array

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
6 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.
  • M Offline
    M Offline
    mmhu
    wrote on last edited by
    #1

    My application needs to store a float number into to a byte array at the specific location. i.e. at byteArray[0] to byteArray[3] will store that float number. wrote : byteArray[0] = ((byte*)&floatNumber)[0]; byteArray[1] = ((byte*)&floatNumber)[1]; byteArray[2] = ((byte*)&floatNumber)[2]; byteArray[3] = ((byte*)&floatNumber)[3]; but after write into a file and read back then cout << (float) byteArray[0] << endl; doesn't get the correct answer. How to modify above code???

    T 1 Reply Last reply
    0
    • M mmhu

      My application needs to store a float number into to a byte array at the specific location. i.e. at byteArray[0] to byteArray[3] will store that float number. wrote : byteArray[0] = ((byte*)&floatNumber)[0]; byteArray[1] = ((byte*)&floatNumber)[1]; byteArray[2] = ((byte*)&floatNumber)[2]; byteArray[3] = ((byte*)&floatNumber)[3]; but after write into a file and read back then cout << (float) byteArray[0] << endl; doesn't get the correct answer. How to modify above code???

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      union FloatToCharArray {
      float f;
      char c[4];
      };

      then, you use it like this :

      FloatToCharArray ftc.f = 4.12;
      cout << "chars are : " << ftc.c[0] << " " << ftc.c[1] << " " << ftc.c[2] << " " << ftc.c[3];


      TOXCCT >>> GEII power

      [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

      M 1 Reply Last reply
      0
      • T toxcct

        union FloatToCharArray {
        float f;
        char c[4];
        };

        then, you use it like this :

        FloatToCharArray ftc.f = 4.12;
        cout << "chars are : " << ftc.c[0] << " " << ftc.c[1] << " " << ftc.c[2] << " " << ftc.c[3];


        TOXCCT >>> GEII power

        [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

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

        I stored ftc.c[0] to byteArray[0]; ftc.c[1] to byteArray[1]; ftc.c[2] to byteArray[2]; ftc.c[3] to byteArray[3]; then output: cout << (float)byteArray[0] << endl; doesn't give the correct number.

        T Z S 3 Replies Last reply
        0
        • M mmhu

          I stored ftc.c[0] to byteArray[0]; ftc.c[1] to byteArray[1]; ftc.c[2] to byteArray[2]; ftc.c[3] to byteArray[3]; then output: cout << (float)byteArray[0] << endl; doesn't give the correct number.

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          of course, it can't work, you are converting only the first char into float, not the 4 chars... use ftc.f (in my union) to retrieve the float...


          TOXCCT >>> GEII power

          [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

          1 Reply Last reply
          0
          • M mmhu

            I stored ftc.c[0] to byteArray[0]; ftc.c[1] to byteArray[1]; ftc.c[2] to byteArray[2]; ftc.c[3] to byteArray[3]; then output: cout << (float)byteArray[0] << endl; doesn't give the correct number.

            Z Offline
            Z Offline
            Zac Howland
            wrote on last edited by
            #5

            This will work a bit better: cout << *((float*)*byteArray[0]) << endl; However, using the union described above, you could write much more clear code:

            union FloatWrapper
            {
            	float myFloat;
            	unsigned char myBytes[4];
            };
            
            FloatWrapper someValue;
            someValue.myFloat = 1.23;
            cout << someValue.myFloat << endl;
            

            If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

            1 Reply Last reply
            0
            • M mmhu

              I stored ftc.c[0] to byteArray[0]; ftc.c[1] to byteArray[1]; ftc.c[2] to byteArray[2]; ftc.c[3] to byteArray[3]; then output: cout << (float)byteArray[0] << endl; doesn't give the correct number.

              S Offline
              S Offline
              shengcheng_jin
              wrote on last edited by
              #6

              Oh you should show the result to us. union good www.codeproject.com is a good web for our programer. I like life and I think it is living.

              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