convertir un int ( ou unsigned char )

Discussion in 'C Programming' started by Andre, Dec 19, 2012.

  1. Andre

    Andre Guest

    to ascii ..
    Qq chose comme ferais
    printf("%d",truc);
    ou plutôt avec scanf(..)
    seulement comme cela doit fonctionner sur u pic16f877 je n'ai pas
    l'espace mémoire pour utiliser scanf.
    Le but est d'afficher l'heure sur un lcd. L'heure étant disponible en
    format UTC.
    Qq un a une idée??
    merci d'avance
    André
    Andre, Dec 19, 2012
    #1
    1. Advertising

  2. Andre wrote:
    > to ascii ..
    > Qq chose comme ferais
    > printf("%d",truc);
    > ou plutôt avec scanf(..)
    > seulement comme cela doit fonctionner sur u pic16f877 je n'ai pas
    > l'espace mémoire pour utiliser scanf.
    > Le but est d'afficher l'heure sur un lcd. L'heure étant disponible en
    > format UTC.
    > Qq un a une idée??
    > merci d'avance
    > André


    Something along these lines? Was used with an atmel uC... Converts
    unsigned... Outputs characters directly to a lcd... The division by ten
    may be a bit costly... not sure how to avoid those...

    <code>

    #include <stdint.h>

    static void print_v(uint32_t value)
    {
    uint8_t s=0,c=9,digit;
    uint32_t cv=1000000000;
    while(c--)
    {
    digit = '0';
    while( value >= cv )
    {
    s=1;
    digit++;// Increment first digit
    value -= cv;
    }
    if(s)
    {
    lcd_putchar(digit,0);
    }
    else lcd_putchar(' ',0);
    cv/=10;
    }
    lcd_putchar('0' + value,0);
    lcd_putchar('\n',0);
    }

    </code>
    Johann Klammer, Dec 19, 2012
    #2
    1. Advertising

  3. Andre

    Shao Miller Guest

    On 12/19/2012 05:37, Andre wrote:
    > to ascii ..
    > Qq chose comme ferais
    > printf("%d",truc);
    > ou plutôt avec scanf(..)
    > seulement comme cela doit fonctionner sur u pic16f877 je n'ai pas
    > l'espace mémoire pour utiliser scanf.
    > Le but est d'afficher l'heure sur un lcd. L'heure étant disponible en
    > format UTC.
    > Qq un a une idée??
    > merci d'avance


    Google translated as:
    > to ascii ..
    > 'd Like sth
    > printf ("% d", foo);
    > or rather with scanf (..)
    > just as it should work on pic16f877 u I do not have the space to use scanf memory.
    > The goal is to display the time on a lcd. Time being available in UTC.
    > Qq one has a idea?
    > thank you in advance


    Do you want 'asctime' in the time.h header? Are you trying to ask a
    user for a decimal number?

    - Shao Miller
    Shao Miller, Dec 19, 2012
    #3
  4. Andre

    Noob Guest

    Andre wrote:
    > to ascii ..
    > Qq chose comme ferais
    > printf("%d",truc);
    > ou plutôt avec scanf(..)
    > seulement comme cela doit fonctionner sur u pic16f877 je n'ai pas
    > l'espace mémoire pour utiliser scanf.
    > Le but est d'afficher l'heure sur un lcd. L'heure étant disponible en
    > format UTC.
    > Qq un a une idée??


    Hello André,

    I think you meant to post to fr.comp.lang.c (where they speak French).

    If you only have to deal with base-10 numbers:

    char convert_val_to_text_representation(char val)
    {
    return val + '0';
    }

    or even a lookup table : char map[10] = "0123456789"

    char c = map[val];

    Regards.
    Noob, Dec 19, 2012
    #4
  5. Andre

    Andre Guest

    Le 19/12/2012 11:53, Shao Miller a écrit :
    > On 12/19/2012 05:37, Andre wrote:
    >> to ascii ..
    >> Qq chose comme ferais
    >> printf("%d",truc);
    >> ou plutôt avec scanf(..)
    >> seulement comme cela doit fonctionner sur u pic16f877 je n'ai pas
    >> l'espace mémoire pour utiliser scanf.
    >> Le but est d'afficher l'heure sur un lcd. L'heure étant disponible en
    >> format UTC.
    >> Qq un a une idée??
    >> merci d'avance

    >
    > Google translated as:
    >> to ascii ..
    >> 'd Like sth
    >> printf ("% d", foo);
    >> or rather with scanf (..)
    >> just as it should work on pic16f877 u I do not have the space to use
    >> scanf memory.
    >> The goal is to display the time on a lcd. Time being available in UTC.
    >> Qq one has a idea?
    >> thank you in advance

    >
    > Do you want 'asctime' in the time.h header? Are you trying to ask a user
    > for a decimal number?
    >
    > - Shao Miller

    Sorry I post this by mistake on the wrong group.
    Many thanks for your answers ( to every body)
    I have the hours as a int (0-24 ) or 0x00 to 0x18 )
    so I need to display this on a LCDn two numbers 1 and 2 if time hour is 12.
    without using scanf as I don't have enough memory to implement scanf on
    a PIC16..
    a lookup table is a solution but that require a lot of memory in the
    'data space ' limited to 368 bytes..

    André
    Andre, Dec 19, 2012
    #5
  6. Andre

    BartC Guest

    "Andre" <> wrote in message
    news:50d19f28$0$3104$...

    > I have the hours as a int (0-24 ) or 0x00 to 0x18 )
    > so I need to display this on a LCDn two numbers 1 and 2 if time hour is
    > 12.
    > without using scanf as I don't have enough memory to implement scanf on
    > #include <stdio.h>

    #include <stdlib.h>

    void showtime(hours) {

    if (hours<10) {
    putchar('0');
    putchar('0'+hours);
    }
    else if (hours<20) {
    putchar('1');
    putchar('0'+(hours-10));
    }
    else {
    putchar('2');
    putchar('0'+(hours-20));
    }
    }

    int main(void) {
    int i;

    for (i=0; i<=24; ++i) {
    showtime(i);
    puts("");
    }
    }

    --
    Bartc
    BartC, Dec 19, 2012
    #6
  7. Andre

    Andre Guest

    Le 19/12/2012 11:37, Andre a écrit :
    > to ascii ..
    > Qq chose comme ferais
    > printf("%d",truc);
    > ou plutôt avec scanf(..)
    > seulement comme cela doit fonctionner sur u pic16f877 je n'ai pas
    > l'espace mémoire pour utiliser scanf.
    > Le but est d'afficher l'heure sur un lcd. L'heure étant disponible en
    > format UTC.
    > Qq un a une idée??
    > merci d'avance
    > André

    Many thanks to everybody..
    André
    Andre, Dec 19, 2012
    #7
  8. Andre

    BartC Guest

    "Andre" <> wrote in message
    news:50d19f28$0$3104$...

    I have the hours as a int (0-24 ) or 0x00 to 0x18 )
    > so I need to display this on a LCDn two numbers 1 and 2 if time hour is
    > 12.
    > without using scanf as I don't have enough memory to implement scanf on a
    > PIC16..


    You keep mentioning scanf. scanf is normally used to convert characters into
    numbers. Do you mean printf? This converts numbers into characters.

    What does the LCD interface need anyway: actual ASCII codes ('0' to '9'),
    BCD codes (0 to 9/15), 7-segment codes, or something else entirely?

    There is anyway another group comp.arch.embedded which I think specialises
    in this.

    --
    Bartc
    BartC, Dec 19, 2012
    #8
  9. "BartC" <> writes:
    > "Andre" <> wrote in message
    > news:50d19f28$0$3104$...
    >
    >> I have the hours as a int (0-24 ) or 0x00 to 0x18 )
    >> so I need to display this on a LCDn two numbers 1 and 2 if time hour is
    >> 12.
    >> without using scanf as I don't have enough memory to implement scanf on
    >> #include <stdio.h>

    > #include <stdlib.h>
    >
    > void showtime(hours) {
    >
    > if (hours<10) {
    > putchar('0');
    > putchar('0'+hours);
    > }
    > else if (hours<20) {
    > putchar('1');
    > putchar('0'+(hours-10));
    > }
    > else {
    > putchar('2');
    > putchar('0'+(hours-20));
    > }
    > }
    >
    > int main(void) {
    > int i;
    >
    > for (i=0; i<=24; ++i) {
    > showtime(i);
    > puts("");
    > }
    > }


    Simpler:

    putchar('0'+hours/10);
    putchar('0'+hours%10);

    Your approach might make sense if integer division is horribly
    expensive, but any performance gains are likely to be lost in the
    time it takes to perform the output.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Dec 19, 2012
    #9
  10. Andre

    BartC Guest

    "Keith Thompson" <> wrote in message
    news:...
    > "BartC" <> writes:
    >> "Andre" <> wrote in message


    >>> I have the hours as a int (0-24 ) or 0x00 to 0x18 )
    >>> so I need to display this on a LCDn two numbers 1 and 2 if time hour is
    >>> 12.


    >> if (hours<10) {
    >> putchar('0');
    >> putchar('0'+hours);
    >> }
    >> else if (hours<20) {
    >> putchar('1');
    >> putchar('0'+(hours-10));
    >> }
    >> else {
    >> putchar('2');
    >> putchar('0'+(hours-20));
    >> }


    > Simpler:
    >
    > putchar('0'+hours/10);
    > putchar('0'+hours%10);
    >
    > Your approach might make sense if integer division is horribly
    > expensive, but any performance gains are likely to be lost in the
    > time it takes to perform the output.


    Yeah...

    I didn't have division in mind at all, unless I was just subconsciously
    avoiding it (this is a PIC after all).

    However lookups will also work, although the OP thinks they will use up data
    memory (ram presumably). But I'd be surprised if the PIC compiler couldn't
    somehow put const data into read-only-memory which is usually more generous.

    --
    Bartc
    BartC, Dec 20, 2012
    #10
  11. Andre

    Phil Carmody Guest

    Keith Thompson <> writes:
    > "BartC" <> writes:
    > > "Andre" <> wrote in message
    > > news:50d19f28$0$3104$...
    > >
    > >> I have the hours as a int (0-24 ) or 0x00 to 0x18 )
    > >> so I need to display this on a LCDn two numbers 1 and 2 if time hour is
    > >> 12.
    > >> without using scanf as I don't have enough memory to implement scanf on
    > >> #include <stdio.h>

    > > #include <stdlib.h>
    > >
    > > void showtime(hours) {
    > >
    > > if (hours<10) {
    > > putchar('0');
    > > putchar('0'+hours);
    > > }
    > > else if (hours<20) {
    > > putchar('1');
    > > putchar('0'+(hours-10));
    > > }
    > > else {
    > > putchar('2');
    > > putchar('0'+(hours-20));
    > > }
    > > }
    > >
    > > int main(void) {
    > > int i;
    > >
    > > for (i=0; i<=24; ++i) {
    > > showtime(i);
    > > puts("");
    > > }
    > > }

    >
    > Simpler:
    >
    > putchar('0'+hours/10);
    > putchar('0'+hours%10);
    >
    > Your approach might make sense if integer division is horribly
    > expensive, but any performance gains are likely to be lost in the
    > time it takes to perform the output.


    If integer division is expensive, most likely from a code-space PoV,
    and I presume space is a more valuable commodity than time, then do
    what everyone did 30 years ago...

    void show2digits(int i) /* precondition 0 <= i <= 99 */
    {
    char tens='0';
    while((i-=10) >= 0) {
    ++tens;
    }
    putchar(tens);
    putchar(i+(10+'0'));
    }

    Depending on how the PIC handles flags, this may be better:

    void show2digits(int i) /* precondition 0 <= i <= 99 */
    {
    char tens='0'-1;
    do { ++tens } while ((i-=10) >= 0);
    putchar(tens);
    putchar(i+(10+'0'));
    }

    Phil
    --
    I'm not saying that google groups censors my posts, but there's a strong link
    between me saying "google groups sucks" in articles, and them disappearing.

    Oh - I guess I might be saying that google groups censors my posts.
    Phil Carmody, Dec 20, 2012
    #11
    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. Timo Freiberger
    Replies:
    3
    Views:
    933
    Bob Hairgrove
    Oct 30, 2004
  2. Hal Styli
    Replies:
    14
    Views:
    1,617
    Old Wolf
    Jan 20, 2004
  3. er
    Replies:
    6
    Views:
    479
    Andre Kostur
    Sep 14, 2007
  4. ciccio

    int*unsigned int = unsigned?

    ciccio, Jun 4, 2010, in forum: C++
    Replies:
    2
    Views:
    396
    Öö Tiib
    Jun 4, 2010
  5. pozz
    Replies:
    12
    Views:
    726
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page