Community
cancel
Showing results for 
Search instead for 
Did you mean: 
JOvie1
Beginner
1,510 Views

char to float on intel galileo gen1

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

{

for(int j=0; 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.

0 Kudos
7 Replies
idata
Community Manager
81 Views

Hello julian403,

 

 

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

 

 

# include

 

# include

 

# include

 

 

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.
JOvie1
Beginner
81 Views

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++.

idata
Community Manager
81 Views

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

 

# include

 

 

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.
FTinetti
Honored Contributor I
81 Views

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.

JOvie1
Beginner
81 Views

[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.

FTinetti
Honored Contributor I
81 Views

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.

idata
Community Manager
81 Views

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.
Reply