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 10 2257
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.
-- Er*********@sun.com
In <3f********@clarion.carno.net.au> 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: Da*****@ifh.de
Rick <rrquick@nospam-com> wrote in message news:<3f********@clarion.carno.net.au>... 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.
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 <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 (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"To know me IS to love me."
- JIPsoft
In <bm**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> 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: Da*****@ifh.de
Dan Pop <Da*****@cern.ch> scribbled the following: In <bm**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> 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 (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You could take his life and..."
- Mirja Tolsa
Joona I Palaste <pa*****@cc.helsinki.fi> wrote: Dan Pop <Da*****@cern.ch> scribbled the following: In <bm**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> 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
(ir*******@freenet.de)
Irrwahn Grausewitz wrote: Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Dan Pop <Da*****@cern.ch> scribbled the following: In <bm**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> 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 <je****@jdyallop.freeserve.co.uk> wrote: Irrwahn Grausewitz wrote: Joona I Palaste <pa*****@cc.helsinki.fi> wrote:Dan Pop <Da*****@cern.ch> 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
(ir*******@freenet.de) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Steve Holden |
last post by:
I was messing about with formatting and realized that the right kind of
object could quite easily tell me exactly what accesses are made to the
mapping in a string % mapping operation. This is a...
|
by: Robert Manookian |
last post by:
How do you format strings? i.e.
In VB6: Format("AB34567", "@@@@@-@@") = "AB345-67"
In .Net: ????????
|
by: David Veeneman |
last post by:
How does one format a date column in a GridView control? I had assumed that
the DataFormat string would do it, but MSDN only shows numeric formatting
codes. Can dates be formatted using that...
|
by: Tomasz J |
last post by:
Hello developers,
I bind my TextBox control specyfying a format stored in my application
global ApplicationContext object - it has a static string CurrencyFormat
property.
The problem - this...
|
by: Jean-Paul Calderone |
last post by:
On Fri, 5 Sep 2008 14:24:16 -0500, Robert Dailey <rcdailey@gmail.comwrote:
mystring = (
"This is a very long string that "
"spans multiple lines and does "
"not include line breaks or tabs "...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |