|
|
|
|
|
|
| Author |
Message |
spibou@gmail.com *nix forums Guru Wannabe
Joined: 29 May 2006
Posts: 122
|
Posted: Mon Jul 17, 2006 10:28 pm Post subject:
Re: convert int to string without using standard library (stdio.h)
|
|
|
ceeques wrote:
| Quote: | Hi
I am a novice in C. Could you guys help me solve this problem -
I need to convert integer(and /short) to string without using sprintf
(I dont have standard libray stdio.h).
for instance:-
int i =2;
char ch 'A
My result should be a string ==> "A2"
|
I don't understand what you're trying to do. Please
provide more examples. Also write down what your
function should accept as arguments and what its
return type should be. And also try to avoid syntax
errors like char ch 'A' ; it makes it harder to see what
your aim is.
| Quote: | Results are not going to a standard output - I have a function to
readout/display string from memory.
|
Ok , so where results should go then ? |
|
| Back to top |
|
 |
Peter Nilsson *nix forums Guru
Joined: 20 Feb 2005
Posts: 399
|
|
| Back to top |
|
 |
ceeques *nix forums beginner
Joined: 17 Jul 2006
Posts: 3
|
Posted: Mon Jul 17, 2006 10:42 pm Post subject:
Re: convert int to string without using standard library (stdio.h)
|
|
|
Thanks for the repy:
I need to convert number (int, short) to a string but cannot use
sprintf as I am using an embedded system. I want to mix text and
number in a display function on the system's output (It doesn't support
printf) but it does have a text to screen function that accepts
strings. I would like to display counters with a message along with it.
Hope my question is clear now.
Thanks in advance,
cQ
spibou@gmail.com wrote:
| Quote: | ceeques wrote:
Hi
I am a novice in C. Could you guys help me solve this problem -
I need to convert integer(and /short) to string without using sprintf
(I dont have standard libray stdio.h).
for instance:-
int i =2;
char ch 'A
My result should be a string ==> "A2"
I don't understand what you're trying to do. Please
provide more examples. Also write down what your
function should accept as arguments and what its
return type should be. And also try to avoid syntax
errors like char ch 'A' ; it makes it harder to see what
your aim is.
Results are not going to a standard output - I have a function to
readout/display string from memory.
Ok , so where results should go then ? |
|
|
| Back to top |
|
 |
pete *nix forums Guru
Joined: 09 Apr 2005
Posts: 1757
|
Posted: Mon Jul 17, 2006 10:48 pm Post subject:
Re: convert int to string without using standard library (stdio.h)
|
|
|
ceeques wrote:
| Quote: |
Hi
I am a novice in C. Could you guys help me solve this problem -
I need to convert integer(and /short) to string without using sprintf
(I dont have standard libray stdio.h).
for instance:-
int i =2;
char ch 'A'
My result should be a string ==> "A2"
Results are not going to a standard output - I have a function to
readout/display string from memory.
|
/* BEGIN new.c */
#include <stdio.h>
void itoa(int n, char *s);
static char *sput_u(unsigned n, char *s);
static char *sput_up1(unsigned n, char *s);
int main(void)
{
char string[3] = "";
int i = 2;
char ch = 'A';
string[0] = ch;
itoa(i, string + 1);
puts(string);
return 0;
}
void itoa(int n, char *s)
{
if (0 > n) {
*s++ = '-';
*sput_up1(-(n + 1), s) = '\0';
} else {
*sput_u(n, s) = '\0';
}
}
static char *sput_u(unsigned n, char *s)
{
unsigned digit, tenth;
tenth = n / 10;
digit = n - 10 * tenth;
if (tenth != 0) {
s = sput_u(tenth, s);
}
*s = (char)(digit + '0');
return s + 1;
}
static char *sput_up1(unsigned n, char *s)
{
unsigned digit, tenth;
tenth = n / 10;
digit = n - 10 * tenth;
if (digit == 9) {
if (tenth != 0) {
s = sput_up1(tenth, s);
} else {
*s++ = '1';
}
*s = '0';
} else {
if (tenth != 0) {
s = sput_u(tenth, s);
}
*s = (char)(digit + '1');
}
return s + 1;
}
/* END new.c */
--
pete |
|
| Back to top |
|
 |
Mark McIntyre *nix forums Guru
Joined: 16 Feb 2005
Posts: 1564
|
Posted: Mon Jul 17, 2006 10:55 pm Post subject:
Re: convert int to string without using standard library (stdio.h)
|
|
|
On 17 Jul 2006 15:42:50 -0700, in comp.lang.c , "ceeques"
<cquestions@gmail.com> wrote:
| Quote: | Thanks for the repy:
I need to convert number (int, short) to a string but cannot use
sprintf as I am using an embedded system. I want to mix text and
number in a display function on the system's output (It doesn't support
printf) but it does have a text to screen function that accepts
strings. I would like to display counters with a message along with it.
Hope my question is clear now.
|
If you have a single-digit number, you can convert it into the
equivalent character by adding '0'
int i = 4;
char x = i + '0'; // x will be '4'
If you have more than one digit, you would have to apply this logic
iteratively over each digit in turn.
To handle floating point types, you'd need to convert to scaled
integers, and re-insert the decimal point as appropriate.
--
Mark McIntyre
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan |
|
| Back to top |
|
 |
Default User *nix forums Guru
Joined: 21 Feb 2005
Posts: 1159
|
Posted: Mon Jul 17, 2006 11:10 pm Post subject:
Re: convert int to string without using standard library (stdio.h)
|
|
|
ceeques wrote:
| Quote: | spibou@gmail.com wrote:
Thanks for the repy:
|
Please don't top-post, your reply belongs following or interspersed
with properly trimmed quotes. See most of the other posts here for
examples.
(text rearranged)
| Quote: | I don't understand what you're trying to do. Please
provide more examples. Also write down what your
function should accept as arguments and what its
return type should be. And also try to avoid syntax
errors like char ch 'A' ; it makes it harder to see what
your aim is.
I need to convert number (int, short) to a string but cannot use
sprintf as I am using an embedded system. I want to mix text and
number in a display function on the system's output (It doesn't
support printf) but it does have a text to screen function that
accepts strings. I would like to display counters with a message
along with it.
|
Review the requests made above. Show the function signature and provide
examples.
Brian |
|
| Back to top |
|
 |
websnarf@gmail.com *nix forums Guru Wannabe
Joined: 26 Jul 2005
Posts: 249
|
Posted: Tue Jul 18, 2006 1:35 am Post subject:
Re: convert int to string without using standard library (stdio.h)
|
|
|
ceeques wrote:
| Quote: | I am a novice in C. Could you guys help me solve this problem -
I need to convert integer(and /short) to string without using sprintf
(I dont have standard libray stdio.h).
for instance:-
int i =2;
char ch 'A'
My result should be a string ==> "A2"
|
See if this can help you:
#include <string.h>
char * itostrappend (char * str, int v, int base) {
char d[2];
int rem = v % base;
if (rem < 0) {
strcat (str, "-");
v = -(v / base);
rem = -rem;
} else v /= base;
d[0] = "0123456789abcdefghijklmnopqrstuvwxyz"[rem];
d[1] = '\0';
if (v) str = itostrappend (str, v, base);
strcat (str, d);
return str + strlen (str);
}
void itostr (char * str, int v, int base) { /* Simple wrapper */
str[0] = '\0';
itostrappend (str, v, base);
}
Its not very fast, but it works, and its performance is O(log(v))
(exercise to the reader.) Unfortunately it also requires O(log(v))
stack space.
--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/ |
|
| Back to top |
|
 |
Old Wolf *nix forums Guru
Joined: 20 Feb 2005
Posts: 679
|
Posted: Tue Jul 18, 2006 3:16 am Post subject:
Re: convert int to string without using standard library (stdio.h)
|
|
|
ceeques wrote:
| Quote: | Hi
I am a novice in C. Could you guys help me solve this problem -
I need to convert integer(and /short) to string without using sprintf
(I dont have standard libray stdio.h).
for instance:-
int i =2;
char ch 'A'
My result should be a string ==> "A2"
|
Double check that you don't have sprintf in your system libraries
(even if perhaps you don't have the header for it). Also, look for
'itoa' in your system libraries.
If not, download a public domain implementation of sprintf. |
|
| Back to top |
|
 |
Old Wolf *nix forums Guru
Joined: 20 Feb 2005
Posts: 679
|
Posted: Tue Jul 18, 2006 3:17 am Post subject:
Re: convert int to string without using standard library (stdio.h)
|
|
|
websnarf@gmail.com wrote:
| Quote: | ceeques wrote:
I need to convert integer(and /short) to string without using sprintf
#include <string.h
char * itostrappend (char * str, int v, int base) {
char d[2];
int rem = v % base;
if (rem < 0) {
strcat (str, "-");
v = -(v / base);
rem = -rem;
} else v /= base;
d[0] = "0123456789abcdefghijklmnopqrstuvwxyz"[rem];
d[1] = '\0';
if (v) str = itostrappend (str, v, base);
strcat (str, d);
return str + strlen (str);
}
void itostr (char * str, int v, int base) { /* Simple wrapper */
str[0] = '\0';
itostrappend (str, v, base);
}
|
Is that supposed to be a serious answer? |
|
| Back to top |
|
 |
Morris Dovey *nix forums addict
Joined: 22 Jun 2005
Posts: 90
|
Posted: Tue Jul 18, 2006 3:18 am Post subject:
Re: convert int to string without using standard library (stdio.h)
|
|
|
ceeques (in 1153176169.958830.264830@m79g2000cwm.googlegroups.com)
said:
| Thanks for the repy:
|
| I need to convert number (int, short) to a string but cannot use
| sprintf as I am using an embedded system. I want to mix text and
| number in a display function on the system's output (It doesn't
| support printf) but it does have a text to screen function that
| accepts strings. I would like to display counters with a message
| along with it.
char *itos(char *s,int i)
{ char *r = s;
int t;
if (i < 0)
{ i = -i;
*s++ = '-';
}
t = i;
do ++s; while (t /= 10);
*s = '\0';
do *--s = '0' + i % 10; while (i /= 10);
return r;
}
Will convert i to a properly terminated digit string beginning at s
and will prepend a '-' if the value of i is less than zero. It will
return a pointer to the start of the string.
--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto |
|
| Back to top |
|
 |
websnarf@gmail.com *nix forums Guru Wannabe
Joined: 26 Jul 2005
Posts: 249
|
Posted: Tue Jul 18, 2006 8:24 am Post subject:
Re: convert int to string without using standard library (stdio.h)
|
|
|
Old Wolf wrote:
| Quote: | Is that supposed to be a serious answer?
|
What's your problem with it?
--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/ |
|
| Back to top |
|
 |
Mark McIntyre *nix forums Guru
Joined: 16 Feb 2005
Posts: 1564
|
Posted: Tue Jul 18, 2006 10:08 am Post subject:
Re: convert int to string without using standard library (stdio.h)
|
|
|
On 18 Jul 2006 01:24:31 -0700, in comp.lang.c , websnarf@gmail.com
wrote:
| Quote: | Old Wolf wrote:
Is that supposed to be a serious answer?
What's your problem with it?
|
Its hideously obfuscated, and completely unsuitable for the OP's level
of understanding. It seems more grandstanding than helpful. Perhaps
you meant it differently.
--
Mark McIntyre
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan |
|
| Back to top |
|
 |
Eric Sosman *nix forums Guru
Joined: 21 Feb 2005
Posts: 1246
|
Posted: Tue Jul 18, 2006 11:36 am Post subject:
Re: convert int to string without using standard library (stdio.h)
|
|
|
Morris Dovey wrote:
| Quote: | char *itos(char *s,int i)
{ char *r = s;
int t;
if (i < 0)
{ i = -i;
*s++ = '-';
}
t = i;
do ++s; while (t /= 10);
*s = '\0';
do *--s = '0' + i % 10; while (i /= 10);
return r;
}
Will convert i to a properly terminated digit string beginning at s
and will prepend a '-' if the value of i is less than zero. It will
return a pointer to the start of the string.
|
Something's strange: On my machine this works most of
the time, but once it gave "-./,),(-*,(" as the digit string.
Any idea why? (The value of i was negative and large when
the peculiar output occurred: -2147483648, to be precise.)
--
Eric Sosman
esosman@acm-dot-org.invalid |
|
| Back to top |
|
 |
websnarf@gmail.com *nix forums Guru Wannabe
Joined: 26 Jul 2005
Posts: 249
|
Posted: Tue Jul 18, 2006 1:54 pm Post subject:
Re: convert int to string without using standard library (stdio.h)
|
|
|
Mark McIntyre wrote:
| Quote: | On 18 Jul 2006 01:24:31 -0700, in comp.lang.c , websnarf@gmail.com
wrote:
Old Wolf wrote:
Is that supposed to be a serious answer?
What's your problem with it?
Its hideously obfuscated,
|
Its too short to be obfuscated. I didn't ram things on one line, and I
didn't use side-effects in unexpected ways, I didn't abuse the
preprocessor, and there is nothing mysterious in the algorithm used.
The algorithm is also extremely clear:
1. Break down the number into last digit and upper digits.
2. Deal with negative numbers, by normalizing to a positive number.
3. Figure out the last digit.
4. Recursively output the upper digits unless they are 0.
5. Append the last digit.
The only thing really tricky is how the normalization happens -- step 2
happens *after* step 1, in order to avoid the 2s complement INT_MIN
problem.
| Quote: | [...] and completely unsuitable for the OP's level
of understanding.
|
What do you know of the OP's level of understanding? Even if it is
over his head, he's got something to study, hasn't he? I don't see
that there's anything in the solution that either the poster doesn't
need for the solution or isn't worthwhile to know anyways.
| Quote: | [...] It seems more grandstanding than helpful.
|
As opposed to one solution being incorrect, another broken down into a
half dozen sub-functions and you going off into some floating point
divergence (I'd like to see *your* solution to FP->char w/o the std
library).
Modulo a missing * in the OP's question, I have answered the OP's
question fairly exactly, and correctly.
--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/ |
|
| Back to top |
|
 |
Morris Dovey *nix forums addict
Joined: 22 Jun 2005
Posts: 90
|
Posted: Tue Jul 18, 2006 2:07 pm Post subject:
Re: convert int to string without using standard library (stdio.h)
|
|
|
Eric Sosman (in acKdnZlwuLw2WiHZnZ2dnUVZ_uydnZ2d@comcast.com) said:
| Morris Dovey wrote:
|
|| char *itos(char *s,int i)
|| { char *r = s;
|| int t;
|| if (i < 0)
|| { i = -i;
|| *s++ = '-';
|| }
|| t = i;
|| do ++s; while (t /= 10);
|| *s = '\0';
|| do *--s = '0' + i % 10; while (i /= 10);
|| return r;
|| }
||
|| Will convert i to a properly terminated digit string beginning at s
|| and will prepend a '-' if the value of i is less than zero. It will
|| return a pointer to the start of the string.
|
| Something's strange: On my machine this works most of
| the time, but once it gave "-./,),(-*,(" as the digit string.
| Any idea why? (The value of i was negative and large when
| the peculiar output occurred: -2147483648, to be precise.)
Interesting indeed. Could it have been a problem with the target
string area not being large enough to contain the result string? Even
at the boundary point where i = -i might produce a problem, I would
still expect to see an initial sequence of digits.
The problem value is 0xFFFFFFFF80000000 - so there might be a problem
with division of the largest int value if your int size is 32.
It is strange.
--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Fri Nov 21, 2008 10:04 pm | All times are GMT
|
|
Loans | Houses for Sale | Mortgages | Pacotes Carnaval Salvador | Credit Cards
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|