468,457 Members | 1,595 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

convert int to string without using standard library (stdio.h)

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.

Thanks,
cQ

Jul 17 '06 #1
30 15380

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 ?

Jul 17 '06 #2
ceeques wrote:
Hi

I am a novice in C.
You have much to learn, and not just about C. ;-)

Start with...

http://groups.google.com/group/comp....=int+to+string

--
Peter

Jul 17 '06 #3
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
sp****@gmail.com wrote:
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 ?
Jul 17 '06 #4
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"

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
Jul 17 '06 #5
On 17 Jul 2006 15:42:50 -0700, in comp.lang.c , "ceeques"
<cq********@gmail.comwrote:
>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
Jul 17 '06 #6
ceeques wrote:
sp****@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)
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
Jul 17 '06 #7
ceeques wrote:
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/

Jul 18 '06 #8
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"
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.

Jul 18 '06 #9
we******@gmail.com wrote:
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?

Jul 18 '06 #10
ceeques (in 11**********************@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
Jul 18 '06 #11
Old Wolf wrote:
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/

Jul 18 '06 #12
On 18 Jul 2006 01:24:31 -0700, in comp.lang.c , we******@gmail.com
wrote:
>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
Jul 18 '06 #13
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.)

--
Eric Sosman
es*****@acm-dot-org.invalid

Jul 18 '06 #14
Mark McIntyre wrote:
On 18 Jul 2006 01:24:31 -0700, in comp.lang.c , we******@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.
[...] 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.
[...] 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/

Jul 18 '06 #15
Eric Sosman (in ac******************************@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
Jul 18 '06 #16


Morris Dovey wrote On 07/18/06 10:07,:
Eric Sosman (in ac******************************@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.
Not so very strange, actually (I was attempting irony,
but apparently only achieved rust-y ...). The problem is
that INT_MIN may have no corresponding positive int value,
so the computation of `-INT_MIN' overflows. What happens
next is up to the implementation, but on two's complement
systems it is often the case that `-INT_MIN' produces the
value INT_MIN all over again. If this happens, `i = -i'
in the code above does not produce a positive value; `i'
will still be large and negative.

The next piece of the puzzle depends on the behavior
of the modulus operator with negative numbers. In C90, it
is conceivable that you could get lucky: `-13 % 10' could
produce 7 as a result, if `-13 / 10' produces -2. But C90
also allows `-13 % 10' to produce -3 and `-13 / 10' to
produce -1. C99 tightened the rules to *require* the latter,
in part for consistency with other languages and in part
because practically every implementation already did it that
way anyhow. So it's almost a sure bet that `negative % 10'
produces a remainder somewhere between -9 and 0.

The weird characters I reported happen to have (on my
system) encodings that lie just before that of the digit '0'.
Strangeness explained.

To make a long story short: "Emit a minus sign and then
convert the absolute value" is *not* a reliable strategy
for converting integers to digit strings.

--
Er*********@sun.com

Jul 18 '06 #17
On 18 Jul 2006 06:54:54 -0700, in comp.lang.c , we******@gmail.com
wrote:
>Mark McIntyre wrote:
>On 18 Jul 2006 01:24:31 -0700, in comp.lang.c , we******@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.
Ya reckon? "somestring"[var] is generally mysterious to newbies;
>The algorithm is also extremely clear:
For martians.
>[...] and completely unsuitable for the OP's level
of understanding.

What do you know of the OP's level of understanding?
I can read his original post. Come on, be sensible.
>Even if it is
over his head, he's got something to study, hasn't he?
Yeah, right, like the GTOR.
>[...] It seems more grandstanding than helpful.

As opposed to one solution being incorrect, another broken down into a
half dozen sub-functions
Like I said, grandstanding. I gave you the benefit before, now I know.
>and you going off into some floating point divergence
You're amusing, in a weird sort of a way.

--
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
Jul 18 '06 #18
Eric Sosman (in 1153233525.240054@news1nwk) said:

| Morris Dovey wrote On 07/18/06 10:07,:
|| Eric Sosman (in ac******************************@comcast.com) said:
||
||| 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.
|
| Not so very strange, actually (I was attempting irony,
| but apparently only achieved rust-y ...). The problem is
| that INT_MIN may have no corresponding positive int value,
| so the computation of `-INT_MIN' overflows. What happens
| next is up to the implementation, but on two's complement
| systems it is often the case that `-INT_MIN' produces the
| value INT_MIN all over again. If this happens, `i = -i'
| in the code above does not produce a positive value; `i'
| will still be large and negative.
|
| The next piece of the puzzle depends on the behavior
| of the modulus operator with negative numbers. In C90, it
| is conceivable that you could get lucky: `-13 % 10' could
| produce 7 as a result, if `-13 / 10' produces -2. But C90
| also allows `-13 % 10' to produce -3 and `-13 / 10' to
| produce -1. C99 tightened the rules to *require* the latter,
| in part for consistency with other languages and in part
| because practically every implementation already did it that
| way anyhow. So it's almost a sure bet that `negative % 10'
| produces a remainder somewhere between -9 and 0.
|
| The weird characters I reported happen to have (on my
| system) encodings that lie just before that of the digit '0'.
| Strangeness explained.
|
| To make a long story short: "Emit a minus sign and then
| convert the absolute value" is *not* a reliable strategy
| for converting integers to digit strings.

I recognized the irony; but was paying closer attention to the
context. The OP is involved in a situation involving a lot of
judgement calls to resolve trade-offs, as illustrated by the decision
to not use printf(). If this boundary value problem is significant in
his judgement, he will need to determine appropriate mods (use
unsigned types, promote to wider type within the function, etc); but
can use the code provided as a starting point.

From where I sit (blissfully ignorant of his project context) all I
can meaningfully offer is the starting point...

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Jul 18 '06 #19
On Mon, 17 Jul 2006 22:06:51 UTC, "ceeques" <cq********@gmail.com>
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"

Results are not going to a standard output - I have a function to
readout/display string from memory.
Get paper and a pen. Do what you describes above by hand.

Remember what you have done and write a function that does exactly the
same.
Do it now. It helps you to understund how programming is done.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Jul 18 '06 #20
Mark McIntyre wrote:
On 18 Jul 2006 06:54:54 -0700, in comp.lang.c , we******@gmail.com
wrote:
Mark McIntyre wrote:
On 18 Jul 2006 01:24:31 -0700, in comp.lang.c , we******@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.

Ya reckon? "somestring"[var] is generally mysterious to newbies;
Its rare, but its not mysterious. Newbie or not, you can understand
what that very quickly. Do you know how many programming languages
support that, BTW?
The algorithm is also extremely clear:

For martians.
Its easy to throw stones, especially when you are setting your own bar
for what you can understand. So show me something that is both clearer
and still correct.
[...] and completely unsuitable for the OP's level
of understanding.
What do you know of the OP's level of understanding?

I can read his original post. Come on, be sensible.
He may not understand C or english, but its not clear to me which. It
turns out the question is actually slightly harder than it seems at
first blush. The other posters here are struggling with it, so perhaps
the OP is simply in the same boat as them, but is actually in pursuit
of a *good* answer.
Even if it is
over his head, he's got something to study, hasn't he?

Yeah, right, like the GTOR.
I don't think says as much about my solution as it does about you.
[...] It seems more grandstanding than helpful.
As opposed to one solution being incorrect, another broken down into a
half dozen sub-functions

Like I said, grandstanding. I gave you the benefit before, now I know.
Well, first of all, you gave me the benefit of nothing. And now you
are basically asserting that correctness and clarity are tantamount to
grandstanding.
and you going off into some floating point divergence

You're amusing, in a weird sort of a way.
You are moronic in an explicit sort of way. Do you understand that
faithful conversion of floating point to a string is a *LOT* harder,
and basically a *DIFFERENT PROBLEM*? In fact I would be interested
(from just a "observe a car crash" sense of interest) in how you would
suggest one might convert a floating point to a string.

Wait a second -- remind me, are you the one that said empty strings are
illegal in C?

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Jul 18 '06 #21
Thanks all for replying once again.

Morris Dovey wrote:
The OP is involved in a situation involving a lot of
judgement calls to resolve trade-offs, as illustrated by the decision
to not use printf(). If this boundary value problem is significant in
his judgement, he will need to determine appropriate mods (use
unsigned types, promote to wider type within the function, etc); but
can use the code provided as a starting point.
I am actually dealing with unsigned integer and unsigned short. Does it
make the situtaion easier to deal with?. Also, if my integer is
represented in hex values does it change anything?..

Old Wolf wrote:
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"

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.
As I said before I don't have a standard library - so i can not use
printf or sprintf. I have one function which can print string to the
embedded system output. so I am trying to convert all unsigned int /
unsigned short to a string.
THANKS!
cQ

Jul 19 '06 #22
ceeques (in 11*********************@s13g2000cwa.googlegroups.c om)
said:

| I am actually dealing with unsigned integer and unsigned short.
| Does it make the situtaion easier to deal with?. Also, if my
| integer is represented in hex values does it change anything?..

Using unsigned types does make it easier. As Eric pointed out, the
function I posted has a problem with the largest negative number -
and by using only unsigned integer types you avoid that problem (and
eliminate the need to deal with taking the absolute value, output of
the sign character, and need for another pointer to char. The code can
be shortened to:

char *itos(char *s,unsigned i)
{ unsigned t = i;
do ++s; while (t /= 10);
*s = '\0';
do *--s = '0' + i % 10; while (i /= 10);
return s;
}

The conversion to hexadecimal digits is slightly different; since you
can use >and & operations instead of / and % operations; and use the
digit values to retrieve digit characters from an array of 16 chars.
Since it's common to display leading zeros with hex numbers, you may
elect to eliminate the first do{}while loop.

The suggestion to download a public domain version of sprintf() is not
a bad idea. On one embedded project, we started with a public domain
version and removed all the features/formats we didn't want to
minimize the memory footprint.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Jul 19 '06 #23
ceeques wrote:
Thanks all for replying once again.
Morris Dovey wrote:
The OP is involved in a situation involving a lot of
judgement calls to resolve trade-offs, as illustrated by the decision
to not use printf(). If this boundary value problem is significant in
his judgement, he will need to determine appropriate mods (use
unsigned types, promote to wider type within the function, etc); but
can use the code provided as a starting point.

I am actually dealing with unsigned integer and unsigned short. Does it
make the situtaion easier to deal with?.
Yes it does. You can drop all the negation checking logic.
[...] Also, if my integer is
represented in hex values does it change anything?..
You can exchange division and modulo with a shift and mask. You can't
use the '0'+lastDigit trick for hexadecimal, of course (this does not
affect my proposed solution.) I would not say that it was technically
*easier*.
Old Wolf wrote:
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.

As I said before I don't have a standard library - so i can not use
printf or sprintf.
Well his point was to go find the source code for sprintf, and just rip
out the part that you need. Unfortunately, its a pretty ignorant
comment, since such code is usually unusually complicated and targetted
towards a different parametrization (you usually pass the field width,
0-extension padding, signedness, right-justification mode, etc
parameters along with your conversion). Its hard to believe that was
given as a serious answer.

I gave you a 12-line solution written way faster than I could mutate
any sprintf source into a correct solution (I wonder how fast you, as a
self-admitted novice, would be able to do the sprintf-source solution).
For just the unsigned cases, Morris Dovey's solution works (though,
his signed int solution is wrong), so you can use that (though I
personally, would generate the string in reverse, then reverse the
string, rather than pay twice the penalty for all the divides and
modulos).
[...] I have one function which can print string to the
embedded system output. so I am trying to convert all unsigned int /
unsigned short to a string.
Right -- so being that you are on an embedded system, do you suppose
that the conciseness of the solution is important?

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Jul 19 '06 #24
On 18 Jul 2006 15:32:37 -0700, in comp.lang.c , we******@gmail.com
wrote:
>Mark McIntyre wrote:
>>
Ya reckon? "somestring"[var] is generally mysterious to newbies;

Its rare, but its not mysterious. Newbie or not, you can understand
what that very quickly.
Hmm, theres probably a reason why its a FAQ.
>The algorithm is also extremely clear:

For martians.

Its easy to throw stones, especially when you are setting your own bar
for what you can understand. So show me something that is both clearer
and still correct.
*shrug*. I already gave the poster a reasonable suggestion that was
both clear and correct.
>What do you know of the OP's level of understanding?

I can read his original post. Come on, be sensible.

He may not understand C or english, but its not clear to me which.
Lets be clear: the OP wanted to know how to output numbers without
using printf. This isn't particularly hard, your fairly confusing
example notwithstanding.
>Like I said, grandstanding. I gave you the benefit before, now I know.

Well, first of all, you gave me the benefit of nothing.
Incorrect
>And now you
are basically asserting that correctness and clarity are tantamount to
grandstanding.
No, I'm asserting that posturing and showing off are tantamount to
grandstanding.
>and you going off into some floating point divergence

You're amusing, in a weird sort of a way.

You are moronic in an explicit sort of way.
And you're neither the front nor the rear of a donkey.
>Do you understand that
faithful conversion of floating point to a string is a *LOT* harder,
and basically a *DIFFERENT PROBLEM*?
I disagree.
>In fact I would be interested
(from just a "observe a car crash" sense of interest) in how you would
suggest one might convert a floating point to a string.
I'm sure you would. Here's a hint: printf has a default precision.
--
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
Jul 19 '06 #25
Mark McIntyre wrote:
On 18 Jul 2006 15:32:37 -0700, in comp.lang.c , we******@gmail.com
wrote:
Mark McIntyre wrote:
Ya reckon? "somestring"[var] is generally mysterious to newbies;
Its rare, but its not mysterious. Newbie or not, you can understand
what that very quickly.

Hmm, theres probably a reason why its a FAQ.
Probably so that newbies could learn it and understand it very quickly.
Although its clearly geared at a very particular kind of newbie --
i.e., one that has not used other languages that also support that
feature.
The algorithm is also extremely clear:

For martians.
Its easy to throw stones, especially when you are setting your own bar
for what you can understand. So show me something that is both clearer
and still correct.

*shrug*. I already gave the poster a reasonable suggestion that was
both clear and correct.
You didn't explain how to extract the digits. Nor did you explain the
problem with negative numbers. How can you be either clear or correct
if you don't describe a complete algorithm?
What do you know of the OP's level of understanding?

I can read his original post. Come on, be sensible.
He may not understand C or english, but its not clear to me which.

Lets be clear: the OP wanted to know how to output numbers without
using printf. This isn't particularly hard, your fairly confusing
example notwithstanding.
Its confusing to you and Old Wolfe, because in a very real sense you
two do not understand the problem. Any complexity that is in my
solution corresponds exactly with the required complexity for solving
the problem. Morris Dovey, for example, was the only one to post a
clearer/simpler solution -- but of course, his initial solution was
wrong (as Eric Sosman also points out.) pete's solution may be correct
(I didn't bother to check it, but I can see what he's *trying* to do)
but it is insanely hard to see that it is correct (I noticed you didn't
make any comment about his solution.) My solution has the benefit of
being correct while being easy to see *why* it is correct.
And now you
are basically asserting that correctness and clarity are tantamount to
grandstanding.

No, I'm asserting that posturing and showing off are tantamount to
grandstanding.
But these attributes are all inside *your* mind. If the problem is so
easy, as you claim, then how is it possible to show off by solving such
an easy problem? The fact is that the problem, as originally stated,
is *slightly* complicated, and was not really satisfactorily answered
by anyone else's post (you could argue that pete's might be ok, but I
still cannot *see* why it works; I would have to run it under a
debugger I think).
Do you understand that
faithful conversion of floating point to a string is a *LOT* harder,
and basically a *DIFFERENT PROBLEM*?

I disagree.
Well you're consistent anyways. Since you haven't even presented a
solution to the original problem, I can see how you are going to easily
subterfuge your way out of this one ...
In fact I would be interested
(from just a "observe a car crash" sense of interest) in how you would
suggest one might convert a floating point to a string.

I'm sure you would. Here's a hint: printf has a default precision.
Well here's a hint for you: for a large enough floating point number f
and integer x the value of (f mod 10^x) is not representable with full
precision in any predetermined length finite data type (similarly for f
* 10^x, for large enough x). So you can try doing it the same way as
you do it for integers, but as typically is the case with you, you will
get an incorrect answer. Your move.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Jul 20 '06 #26
On 19 Jul 2006 18:35:01 -0700, in comp.lang.c , we******@gmail.com
wrote:
>Mark McIntyre wrote:
>On 18 Jul 2006 15:32:37 -0700, in comp.lang.c , we******@gmail.com
wrote:
>Mark McIntyre wrote:
Ya reckon? "somestring"[var] is generally mysterious to newbies;

Its rare, but its not mysterious. Newbie or not, you can understand
what that very quickly.

Hmm, theres probably a reason why its a FAQ.

Probably so that newbies could learn it and understand it very quickly.
Er no. A FAQ is a Frequently ASKED question. That is to say, its
something that people often find confusing....
Although its clearly geared at a very particular kind of newbie --
Yup, ones who are new to C.
>*shrug*. I already gave the poster a reasonable suggestion that was
both clear and correct.

You didn't explain how to extract the digits. Nor did you explain the
problem with negative numbers. How can you be either clear or correct
if you don't describe a complete algorithm?
Firstly, completeness is nothing to do with clarity and correctness.
You know this, but are being awkward.

Secondly, we have a tradition in CLC of giving people enough
iformation to work out the rest themselves. You know this, and are
being disingenuous.
>Its confusing to you and Old Wolfe, because in a very real sense you
two do not understand the problem.
Pompous git. You really are a complete birk aren't you?
>I'm sure you would. Here's a hint: printf has a default precision.

Well here's a hint for you: for a large enough floating point number f
and integer x the value of (f mod 10^x) is not representable with full
precision in any predetermined length finite data type
Apparently you don't understand the problem or the solution after all.
>(similarly for f
* 10^x, for large enough x). So you can try doing it the same way as
you do it for integers, but as typically is the case with you, you will
get an incorrect answer. Your move.
I'm not interested in playing "my dick is bigger than yours" thanks.
--
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
Jul 20 '06 #27
Mark McIntyre wrote:
On 19 Jul 2006 18:35:01 -0700, in comp.lang.c , we******@gmail.com
wrote:
Mark McIntyre wrote:
On 18 Jul 2006 15:32:37 -0700, in comp.lang.c , we******@gmail.com
wrote:
Mark McIntyre wrote:
Ya reckon? "somestring"[var] is generally mysterious to newbies;

Its rare, but its not mysterious. Newbie or not, you can understand
what that very quickly.

Hmm, theres probably a reason why its a FAQ.
Probably so that newbies could learn it and understand it very quickly.

Er no. A FAQ is a Frequently ASKED question.
That's what the letters stand for, but that's not what it is. Why
doesn't the C FAQ supply an answer for splitting strings by characters,
since that's asked here over and over. It doesn't even discuss strtok
at all (I've answered questions about this function itself a half dozen
times here in the last year.) In fact I am not aware of any FAQ in
existence which is actually a collection of frequently asked questions.
Its just a moniker.

Anyone who isn't completely new to the internet ought to know this
(BTW, this is not "an internet", its a news message, and no it did not
arrive through a series of tubes).
[...] That is to say, its
something that people often find confusing....
So you think Steve Summit did the research to actually find out what
people find confusing? Do you think he maintains counts on the number
of times any particular kind of question is asked?
Although its clearly geared at a very particular kind of newbie --

Yup, ones who are new to C.
That's simply not so. An experienced Java, Python or Lua programmer,
who was learning C for the first time would not even notice anything
odd by the construct: "<string>"[index].
*shrug*. I already gave the poster a reasonable suggestion that was
both clear and correct.
You didn't explain how to extract the digits. Nor did you explain the
problem with negative numbers. How can you be either clear or correct
if you don't describe a complete algorithm?

Firstly, completeness is nothing to do with clarity and correctness.
So you occupy a position that is simultaneously clear, correct and yet
ambiguous and incomplete? You have not demonstrated that your approach
*will* work. For example, it *DOESN'T* work with floating point
numbers. That's like describing some attributes about how a perpetual
motion machine might function without giving the details of how to
build one.
You know this, but are being awkward.
No, I'm asserting that pretending that you are clear or correct is
meaningless when talking about something that isn't well defined. In
this case your proposed answer.
Secondly, we have a tradition in CLC of giving people enough
iformation to work out the rest themselves. You know this, and are
being disingenuous.
Yeah, sieg heil! Anyways, *I* have a tradition of removing ambiguity
and trying to provide *REAL* clarity. And I point out mistakes,
especially if they appear hidden or have any reasonable chance of being
misleading (such as this case). I really don't care what the rest of
you sheep do in CLC, except where it clearly acts against this.

Your solution isn't a solution at all, so I just treated it as line
noise (which has a similar degree of clarity and correctness), as I am
sure the OP did as well. Its the other solutions that posed the
problem, because they are close to correct (or difficult to verify as
correct), and the OP could easily have been lead astray. My solution,
in a sense, simultaneously demonstrates why all the other "solutions"
have problems.

So if you have a problem with my approach to posting here, I am going
to suggest that its your problem, as I have no intention of modifying
the way I do things.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Jul 21 '06 #28
we******@gmail.com said:

<reasonable arguments snipped>
Yeah, sieg heil!
Right up until that point, you had my sympathy.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 21 '06 #29
we******@gmail.com writes:
Mark McIntyre wrote:
[...]
>Secondly, we have a tradition in CLC of giving people enough
iformation to work out the rest themselves. You know this, and are
being disingenuous.

Yeah, sieg heil!
Shove it, Paul.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 21 '06 #30
we******@gmail.com wrote:

So if you have a problem with my approach to posting here, I am going
to suggest that its your problem, as I have no intention of modifying
the way I do things.


Fortunately, that can be remedied from my standpoint.

*plonk*


Brian
Jul 21 '06 #31

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Casper Bang | last post: by
97 posts views Thread by s | last post: by
29 posts views Thread by Ajay | last post: by
5 posts views Thread by int main(void) | last post: by
7 posts views Thread by elliotng.ee | last post: by
232 posts views Thread by robert maas, see http://tinyurl.com/uh3t | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.