float to unsigned char array

C

C. Sengstock

Hi,
i want to convert a floating point number to a character array, because I
want to save the number in an NVSRAM which can be described as:
unsigned char RAM[size];

Is there a C typical approach to convert the float number?

I used "fcvt()" to convert float to a string. But it seems its not part of
the standart language.

Thanks, Chris
 
T

Thomas Pfaff

Thomas Pfaff said:
See strtod() (C99 also defines strtof()).

Sorry, but that doesn't quite do what you asked for. Sigh.

See sprintf or snprintf (C99).
 
M

Martin Ambuhl

C. Sengstock said:
Hi,
i want to convert a floating point number to a character array, because I
want to save the number in an NVSRAM which can be described as:

unsigned char RAM[size];
#include <stdio.h>
#include <float.h>


int main(void)
{
char s[BUFSIZ];
sprintf(s, "%.*g", DBL_DIG, 0.00000037);
printf("Here's one such string: %s\n", s);
sprintf(s, "%.*g", DBL_DIG, 0.00037);
printf(" here's another: %s\n", s);
sprintf(s, "%.*g", DBL_DIG, 0.37);
printf(" here's another: %s\n", s);
sprintf(s, "%.*g", DBL_DIG, 370.);
printf(" here's another: %s\n", s);
sprintf(s, "%.*g", DBL_DIG, 370000.);
printf(" here's another: %s\n", s);
sprintf(s, "%.*g", DBL_DIG, 370000000.);
printf(" here's another: %s\n", s);
sprintf(s, "%.*g", DBL_DIG, 370000000000.);
printf(" here's the last: %s\n", s);
return 0;
}


Here's one such string: 3.7e-07
here's another: 0.00037
here's another: 0.37
here's another: 370
here's another: 370000
here's another: 370000000
here's the last: 370000000000
 
E

Emmanuel Delahaye

C. Sengstock said:
i want to convert a floating point number to a character array, because I

I assume you mean a 'string'.
want to save the number in an NVSRAM which can be described as:
unsigned char RAM[size];

Is there a C typical approach to convert the float number?

I used "fcvt()" to convert float to a string. But it seems its not part of
the standart language.

What's wrong with sprintf() (or snprintf() if you have C99). Time to reopen
your C-book...
 
J

Jeff

C. Sengstock said:
Hi,
i want to convert a floating point number to a character array, because I
want to save the number in an NVSRAM which can be described as:
unsigned char RAM[size];

Is there a C typical approach to convert the float number?

Use sprintf( )

Make sure that your char buffer is big enough to hold the result. (If your
compiler support C99, you can use snprintf( ) to avoid buffer overflow)
 
K

Kevin Easton

C. Sengstock said:
Hi,
i want to convert a floating point number to a character array, because I
want to save the number in an NVSRAM which can be described as:
unsigned char RAM[size];

Is there a C typical approach to convert the float number?

I used "fcvt()" to convert float to a string. But it seems its not part of
the standart language.

You can just use memcpy(), as long as you're not moving your NVSRAM
between systems.

- Kevin.
 
Joined
Feb 2, 2008
Messages
1
Reaction score
0
Re: Convert Float to Array

Thanks Members of VelocityReview :), In Winƒ32 I 've just write a program to make a pocket caculator. For sprintf function in <float.h>. Could you tell me how can we know these functions?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top