float to unsigned char array

Discussion in 'C Programming' started by C. Sengstock, Sep 15, 2003.

  1. C. Sengstock

    C. Sengstock Guest

    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
    C. Sengstock, Sep 15, 2003
    #1
    1. Advertising

  2. C. Sengstock

    Thomas Pfaff Guest

    "C. Sengstock" <-heidelberg.de> writes:

    > Hi,
    > i want to convert a floating point number to a character array,


    See strtod() (C99 also defines strtof()).
    Thomas Pfaff, Sep 15, 2003
    #2
    1. Advertising

  3. C. Sengstock

    Thomas Pfaff Guest

    Thomas Pfaff <> writes:

    > "C. Sengstock" <-heidelberg.de> writes:
    >
    > > Hi,
    > > i want to convert a floating point number to a character array,

    >
    > See strtod() (C99 also defines strtof()).


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

    See sprintf or snprintf (C99).
    Thomas Pfaff, Sep 15, 2003
    #3
  4. C. Sengstock wrote:

    > 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


    --
    Martin Ambuhl
    Martin Ambuhl, Sep 15, 2003
    #4
  5. Thomas Pfaff wrote:

    > "C. Sengstock" <-heidelberg.de> writes:
    >
    >
    >>Hi,
    >>i want to convert a floating point number to a character array,

    >
    >
    > See strtod() (C99 also defines strtof()).



    Reread it, Tom. He wants to go the other way, for which sprintf() is the
    choice.

    --
    Martin Ambuhl
    Martin Ambuhl, Sep 15, 2003
    #5
  6. In 'comp.lang.c', "C. Sengstock" <-heidelberg.de> wrote:

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

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    <blank line>
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
    Emmanuel Delahaye, Sep 15, 2003
    #6
  7. C. Sengstock

    Jeff Guest

    "C. Sengstock" <-heidelberg.de> wrote in message
    news:bk4srm$sbo$-heidelberg.de...
    > 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)


    --
    Jeff
    -je6543 at yahoo.com
    Jeff, Sep 16, 2003
    #7
  8. C. Sengstock

    Kevin Easton Guest

    C. Sengstock <-heidelberg.de> wrote:
    > 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.
    Kevin Easton, Sep 16, 2003
    #8
  9. C. Sengstock

    profchan

    Joined:
    Feb 2, 2008
    Messages:
    1
    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?
    profchan, Feb 2, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bd
    Replies:
    0
    Views:
    596
  2. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    562
    Jack Klein
    May 9, 2005
  3. Alex Vinokur
    Replies:
    9
    Views:
    762
    James Kanze
    Oct 13, 2008
  4. Gabriel Genellina
    Replies:
    0
    Views:
    499
    Gabriel Genellina
    Aug 13, 2009
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,486
    James Kanze
    Oct 8, 2009
Loading...

Share This Page