formatting strings

R

Rick

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
 
E

Eric Sosman

Rick said:
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.
 
D

Dan Pop

In said:
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
 
D

David Resnick

Rick said:
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.
 
R

Rick

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

Rick



Eric said:
Rick said:
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.
 
J

Joona I Palaste

Rick said:
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.
 
D

Dan Pop

In said:
Rick said:
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
 
J

Joona I Palaste

Dan Pop said:
In said:
Rick said:
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.
 
I

Irrwahn Grausewitz

Joona I Palaste said:
Dan Pop said:
In <[email protected]> Joona I Palaste <[email protected]> writes:
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? ;-)
 
J

Jeremy Yallop

Irrwahn said:
Joona I Palaste said:
Dan Pop said:
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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top