formatting strings

Discussion in 'C Programming' started by Rick, Oct 16, 2003.

  1. Rick

    Rick Guest

    Hi,

    Other than "sprintf", is there some way we can easily format a string
    composed of, say, integers? I know this might become off topic but I'm
    using (an old version of) avr-gcc which does not support sprintf and I
    need to patch up a string with a few integers and I dont want to do 10
    strcat()'s.

    I have a struct containing 3 ints (a struct representing time) and I
    need to print the "hour" "minute" and "second" fields as: hh:mm:ss. Is
    that possible without a combination of strcats and strcpys. Thanks

    Rick
     
    Rick, Oct 16, 2003
    #1
    1. Advertising

  2. Rick

    Eric Sosman Guest

    Rick wrote:
    >
    > Hi,
    >
    > Other than "sprintf", is there some way we can easily format a string
    > composed of, say, integers? I know this might become off topic but I'm
    > using (an old version of) avr-gcc which does not support sprintf and I
    > need to patch up a string with a few integers and I dont want to do 10
    > strcat()'s.
    >
    > I have a struct containing 3 ints (a struct representing time) and I
    > need to print the "hour" "minute" and "second" fields as: hh:mm:ss. Is
    > that possible without a combination of strcats and strcpys. Thanks


    Incoming values assumed sensible:

    char result[sizeof "hh:mm:ss"];
    char *p = result;
    *p++ = '0' + hh / 10;
    *p++ = '0' + hh % 10;
    *p++ = ':';
    *p++ = '0' + mm / 10;
    *p++ = '0' + mm % 10;
    *p++ = ':';
    *p++ = '0' + ss / 10;
    *p++ = '0' + ss % 10;
    *p = '\0';

    The `p' variable could be eliminated if desired.

    --
     
    Eric Sosman, Oct 16, 2003
    #2
    1. Advertising

  3. Rick

    Dan Pop Guest

    In <3f8ea2c2$> Rick <rrquick@nospam-com> writes:

    >Other than "sprintf", is there some way we can easily format a string
    >composed of, say, integers?


    Integers are very easy to convert to strings.

    >I know this might become off topic but I'm
    >using (an old version of) avr-gcc which does not support sprintf and I
    >need to patch up a string with a few integers and I dont want to do 10
    >strcat()'s.


    You don't do that with strcat, you simply put each digit at its place
    in the string.

    >I have a struct containing 3 ints (a struct representing time) and I
    >need to print the "hour" "minute" and "second" fields as: hh:mm:ss. Is
    >that possible without a combination of strcats and strcpys. Thanks


    Of course it's possible, it's a matter of *trivial* arithmetic:

    char timestr[9];
    timestr[0] = '0' + hour / 10;
    timestr[1] = '0' + hour % 10;
    timestr[2] = ':';
    ...
    timestr[8] = 0;

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Oct 16, 2003
    #3
  4. Rick <rrquick@nospam-com> wrote in message news:<3f8ea2c2$>...
    > Hi,
    >
    > Other than "sprintf", is there some way we can easily format a string
    > composed of, say, integers? I know this might become off topic but I'm
    > using (an old version of) avr-gcc which does not support sprintf and I
    > need to patch up a string with a few integers and I dont want to do 10
    > strcat()'s.
    >
    > I have a struct containing 3 ints (a struct representing time) and I
    > need to print the "hour" "minute" and "second" fields as: hh:mm:ss. Is
    > that possible without a combination of strcats and strcpys. Thanks
    >
    > Rick


    #include <stdio.h>

    void format_hms(int hour, int minute, int second, char *buf)
    {
    buf[0] = hour/10 + '0';
    buf[1] = hour%10 + '0';
    buf[2] = ':';
    buf[3] = minute/10 + '0';
    buf[4] = minute%10 + '0';
    buf[5] = ':';
    buf[6] = second/10 + '0';
    buf[7] = second%10 + '0';
    buf[8] = '\0';
    }


    int main(void)
    {
    char buf[9];
    format_hms(10,20,30,buf);
    printf("%s\n", buf);
    return 0;
    }

    Eliminating the code duplication, adding error checking, etc would
    improve this, but it is a rough idea of something that works.
     
    David Resnick, Oct 16, 2003
    #4
  5. Rick

    Rick Guest

    Thanks Eric.. just wondering.. what does '0' + hh do? Will it convert
    the integer into ascii? Neat :)

    Rick



    Eric Sosman wrote:
    > Rick wrote:
    >
    >>Hi,
    >>
    >>Other than "sprintf", is there some way we can easily format a string
    >>composed of, say, integers? I know this might become off topic but I'm
    >>using (an old version of) avr-gcc which does not support sprintf and I
    >>need to patch up a string with a few integers and I dont want to do 10
    >>strcat()'s.
    >>
    >>I have a struct containing 3 ints (a struct representing time) and I
    >>need to print the "hour" "minute" and "second" fields as: hh:mm:ss. Is
    >>that possible without a combination of strcats and strcpys. Thanks

    >
    >
    > Incoming values assumed sensible:
    >
    > char result[sizeof "hh:mm:ss"];
    > char *p = result;
    > *p++ = '0' + hh / 10;
    > *p++ = '0' + hh % 10;
    > *p++ = ':';
    > *p++ = '0' + mm / 10;
    > *p++ = '0' + mm % 10;
    > *p++ = ':';
    > *p++ = '0' + ss / 10;
    > *p++ = '0' + ss % 10;
    > *p = '\0';
    >
    > The `p' variable could be eliminated if desired.
    >
     
    Rick, Oct 17, 2003
    #5
  6. Rick <rrquick@nospam-com> scribbled the following:
    > Thanks Eric.. just wondering.. what does '0' + hh do? Will it convert
    > the integer into ascii? Neat :)


    No. If hh is in the range [0, 9] it will produce the corresponding
    character glyph in whatever character set the implementation is using.
    This can be ASCII or some other set.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "To know me IS to love me."
    - JIPsoft
     
    Joona I Palaste, Oct 17, 2003
    #6
  7. Rick

    Dan Pop Guest

    In <bmoa9o$p7r$> Joona I Palaste <> writes:

    >Rick <rrquick@nospam-com> scribbled the following:
    >> Thanks Eric.. just wondering.. what does '0' + hh do? Will it convert
    >> the integer into ascii? Neat :)

    >
    >No. If hh is in the range [0, 9] it will produce the corresponding
    >character glyph in whatever character set the implementation is using.


    Wrong! It will produce the corresponding character *code*. You need to
    output this code to the right kind of device in order to get a glyph.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Oct 17, 2003
    #7
  8. Dan Pop <> scribbled the following:
    > In <bmoa9o$p7r$> Joona I Palaste <> writes:
    >>Rick <rrquick@nospam-com> scribbled the following:
    >>> Thanks Eric.. just wondering.. what does '0' + hh do? Will it convert
    >>> the integer into ascii? Neat :)

    >>
    >>No. If hh is in the range [0, 9] it will produce the corresponding
    >>character glyph in whatever character set the implementation is using.


    > Wrong! It will produce the corresponding character *code*. You need to
    > output this code to the right kind of device in order to get a glyph.


    You have to very pedantic to make that kind of argument. But I suppose
    that if we get pedantic enough, you are absolutely correct.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "You could take his life and..."
    - Mirja Tolsa
     
    Joona I Palaste, Oct 17, 2003
    #8
  9. Joona I Palaste <> wrote:

    >Dan Pop <> scribbled the following:
    >> In <bmoa9o$p7r$> Joona I Palaste <> writes:

    <snip>
    >>>No. If hh is in the range [0, 9] it will produce the corresponding
    >>>character glyph in whatever character set the implementation is using.

    >
    >> Wrong! It will produce the corresponding character *code*. You need to
    >> output this code to the right kind of device in order to get a glyph.

    >
    >You have to very pedantic to make that kind of argument. But I suppose
    >that if we get pedantic enough, you are absolutely correct.


    Huh?!? He's not correct if we aren't pedantic enough? ;-)

    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 17, 2003
    #9
  10. Irrwahn Grausewitz wrote:
    > Joona I Palaste <> wrote:
    >
    >>Dan Pop <> scribbled the following:
    >>> In <bmoa9o$p7r$> Joona I Palaste <> writes:

    ><snip>
    >>>>No. If hh is in the range [0, 9] it will produce the corresponding
    >>>>character glyph in whatever character set the implementation is using.

    >>
    >>> Wrong! It will produce the corresponding character *code*. You need to
    >>> output this code to the right kind of device in order to get a glyph.

    >>
    >>You have to very pedantic to make that kind of argument. But I suppose
    >>that if we get pedantic enough, you are absolutely correct.

    >
    > Huh?!? He's not correct if we aren't pedantic enough? ;-)


    Er, a => b does not imply !a => !b.

    Jeremy.
     
    Jeremy Yallop, Oct 17, 2003
    #10
  11. Jeremy Yallop <> wrote:

    >Irrwahn Grausewitz wrote:
    >> Joona I Palaste <> wrote:
    >>>Dan Pop <> scribbled the following:

    <snip>
    >>>> Wrong! It will produce the corresponding character *code*. You need to
    >>>> output this code to the right kind of device in order to get a glyph.
    >>>
    >>>You have to very pedantic to make that kind of argument. But I suppose
    >>>that if we get pedantic enough, you are absolutely correct.

    >>
    >> Huh?!? He's not correct if we aren't pedantic enough? ;-)

    >
    >Er, a => b does not imply !a => !b.


    Hm, right. But still then, I miss Joona's point.
    Maybe he made a joke after all. ;-)

    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 17, 2003
    #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. Perecli Manole

    formatting strings

    Perecli Manole, Oct 23, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    550
    Bret Mulvey [MS]
    Oct 24, 2003
  2. Guadala Harry

    Formatting Strings

    Guadala Harry, Oct 28, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    393
    Guadala Harry
    Oct 28, 2004
  3. Joona I Palaste

    Question on formatting Strings

    Joona I Palaste, Oct 19, 2003, in forum: Java
    Replies:
    3
    Views:
    394
    Paul Lutus
    Oct 20, 2003
  4. Steve Holden

    Enumerating formatting strings

    Steve Holden, Apr 18, 2005, in forum: Python
    Replies:
    14
    Views:
    513
    Michael Spencer
    Apr 21, 2005
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    767
    Malcolm
    Jun 24, 2006
Loading...

Share This Page