Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > formatting strings

Reply
Thread Tools

formatting strings

 
 
Rick
Guest
Posts: n/a
 
      10-16-2003
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

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      10-16-2003
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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Dan Pop
Guest
Posts: n/a
 
      10-16-2003
In <3f8ea2c2$(E-Mail Removed)> 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: (E-Mail Removed)
 
Reply With Quote
 
David Resnick
Guest
Posts: n/a
 
      10-16-2003
Rick <rrquick@nospam-com> wrote in message news:<3f8ea2c2$(E-Mail Removed)>...
> 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.
 
Reply With Quote
 
Rick
Guest
Posts: n/a
 
      10-17-2003
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.
>


 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      10-17-2003
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 ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"To know me IS to love me."
- JIPsoft
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      10-17-2003
In <bmoa9o$p7r$(E-Mail Removed)> Joona I Palaste <(E-Mail Removed)> 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: (E-Mail Removed)
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      10-17-2003
Dan Pop <(E-Mail Removed)> scribbled the following:
> In <bmoa9o$p7r$(E-Mail Removed)> Joona I Palaste <(E-Mail Removed)> 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 ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You could take his life and..."
- Mirja Tolsa
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-17-2003
Joona I Palaste <(E-Mail Removed)> wrote:

>Dan Pop <(E-Mail Removed)> scribbled the following:
>> In <bmoa9o$p7r$(E-Mail Removed)> Joona I Palaste <(E-Mail Removed)> 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
((E-Mail Removed))
 
Reply With Quote
 
Jeremy Yallop
Guest
Posts: n/a
 
      10-17-2003
Irrwahn Grausewitz wrote:
> Joona I Palaste <(E-Mail Removed)> wrote:
>
>>Dan Pop <(E-Mail Removed)> scribbled the following:
>>> In <bmoa9o$p7r$(E-Mail Removed)> Joona I Palaste <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Formatting Strings Guadala Harry ASP .Net 2 10-28-2004 06:54 PM
Need formatting options menu for formatting hard drive Mark T. Computer Support 3 11-24-2003 11:50 PM
formatting strings Perecli Manole ASP .Net 3 10-24-2003 05:45 AM
Question on formatting Strings Joona I Palaste Java 3 10-20-2003 12:05 AM



Advertisments