7 Replies Latest reply on Jul 29, 2016 1:33 PM by Intel Corporation

    char to float on intel galileo gen1

    julian403

      I have a char array (matrix), which is a buffer from sound card's ADC.

       

      The problem is that I have to calculate the average of all arrays' value and as it's from sound card's adc it has number from -127 to 127, that is to say that a char fall short. So I have to convert char to float. I do the next:

       

      [code]     float average;

      for(int i=0; i<line; i++)

      {

      for(int j=0; j<colum; j++)

      {

      average+= (float) buffer [i][j];

      }}

       

      average/=(line+colum);

      [/code]

       

      Then to know if it's Ok, I keep it in a file and watch it then with security shell.

       

      fprintf(archivo, "%f", average);

       

      where archivo is an FILE type

       

      But it do not work. Why?

       

      How can I convert the buffer values which are char to float? by the way I'm using Intel System Studio IoT

       

      Thanks for yours attention and sorry for my english.  If it help I can adjunt the proyect, it's not too large.

       

       

        • 1. Re: char to float on intel galileo gen1
          Intel Corporation
          This message was posted on behalf of Intel Corporation

          Hello julian403,

          This short code worked great for me. Test it and let me know if it works for you:

          #include <stdio.h>
          #include <stdlib.h>
          #include <dataing.h>

          int main()
          {
              float value;
              char data[20];
             
              strcpy(data, "1234");
              value = atof(data);
              printf("data value = %s, float value = %f\n", data, value);

              return(0);
          }

          -Peter.

          • 2. Re: char to float on intel galileo gen1
            julian403

            I did what you said but it did not work. But I changed the compiller from C++ to C (what I really do was create a new proyect in C instead C++) and it works!! but Why? because I'm doing a big proyects and I will have to use C++ because the classes.

             

            what I did it's

             

            float average=0;

            float aux;

             

            for(int i=0; i<10; i++)

            for(int j=0; j<32;j++)

             

            {

            aux= average;

            average= aux+ (float) buffer[i][j]);

            }

             

            So, Im' thinking that that's cast do not work on C++.

            • 3. Re: char to float on intel galileo gen1
              Intel Corporation
              This message was posted on behalf of Intel Corporation

              I originally thought you were programming on C, that's why my code is in C not C++. The reason why it might have failed on C++ is because the library might not exist on C++.

              Nevertheless, this is the equivalent code in C++:

              #include <stdio.h>
              #include <stdlib.h>

              int main ()
              {
                char data[] = "1234";
                float value;

                value = strtof (data, NULL);
                printf ("The value of %s in float is: %f \n", data, value);
                
                return 0;
              }

              -Peter.

              • 4. Re: char to float on intel galileo gen1
                FGT

                Hi julian403,

                I did what you said but it did not work.

                What do you mean by "not work"? Did it failed to compile? Did it failed to provide the right result? Other?

                 

                Fernando.

                • 5. Re: char to float on intel galileo gen1
                  julian403

                  [qoute]

                  What do you mean by "not work"? Did it failed to compile? Did it failed to provide the right result? Other?

                   

                  [/quote]

                   

                  It's compile but when I copy it to a file, there is nothing on the file.

                  • 6. Re: char to float on intel galileo gen1
                    FGT

                    julian403 wrote:

                     

                    [qoute]

                    What do you mean by "not work"? Did it failed to compile? Did it failed to provide the right result? Other?

                     

                    [/quote]

                     

                    It's compile but when I copy it to a file, there is nothing on the file.

                    I do not understand... would you please explain? Are you using any IDE?

                     

                    Fernando.

                    • 7. Re: char to float on intel galileo gen1
                      Intel Corporation
                      This message was posted on behalf of Intel Corporation

                      Did neither of those codes helped you? I tested both of them and they worked for me just as expected. I compiled them with the commands gcc string_to_float.c -o string_to_float and g++ string_to_float.cpp -o string_to_float_cpp on C and C++ respectively. I did it directly on Linux, I did not use any IDE.

                      -Peter.

                      1 of 1 people found this helpful