integer to characters

A

Alan

I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"

thx
 
K

Karl Heinz Buchegger

Alan said:
I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"

You don't want to change anything.
You want to convert an integer into its textual representation.

char Buffer[20];
int Number = 123;
sprintf( Buffer, "%d", Number );

Also: When posting to alt.comp.lang.learn.c-c++ always
indicate which language you need a solution for. There
we discuss C and C++. The C++ solution would be completely
different.
 
A

Alan

Karl Heinz Buchegger said:
I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"

You don't want to change anything.
You want to convert an integer into its textual representation.

char Buffer[20];
int Number = 123;
sprintf( Buffer, "%d", Number );

Is there any function same as sprintf() but write to file?
because I can't find a function called "fsprintf()"
thx

Also: When posting to alt.comp.lang.learn.c-c++ always
indicate which language you need a solution for. There
we discuss C and C++. The C++ solution would be completely
different.

sorry, next time I will do so
 
R

Rick

Alan said:
I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"

thx

char arr[4];
int val = 123;

arr[0] = '0' + val/100;
arr[1] = '0' + (val%100)/10;
arr[2] = '0' + val%10;
arr[3] = '\0';

printf("%s", arr);


Rick
 
K

Karl Heinz Buchegger

Alan said:
Karl Heinz Buchegger said:
I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"

You don't want to change anything.
You want to convert an integer into its textual representation.

char Buffer[20];
int Number = 123;
sprintf( Buffer, "%d", Number );

Is there any function same as sprintf() but write to file?
because I can't find a function called "fsprintf()"
thx

What for?
Either fprintf() does what you need
or you first print to a character buffer and then
write that character buffer to the file.

In any case: there is no need for fsprintf
 
R

Robert W Hand

Alan said:
I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"

thx

char arr[4];
int val = 123;

arr[0] = '0' + val/100;
arr[1] = '0' + (val%100)/10;
arr[2] = '0' + val%10;
arr[3] = '\0';

printf("%s", arr);

What is the plan if the three-digit integer is negative?

Best wishes,

Bob
 
S

Stefan Höhne

Hi,


Rick said:
Alan said:
I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"

thx

char arr[4];
int val = 123;

arr[0] = '0' + val/100;
arr[1] = '0' + (val%100)/10;
arr[2] = '0' + val%10;
arr[3] = '\0';

printf("%s", arr);

this fails the OPs spec.

Why not use the standard library for things it was
made for?

Regards,
Stefan.
 
A

Alan

Stefan Höhne said:
Hi,

Rick said:
Alan said:
I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"

thx

char arr[4];
int val = 123;

arr[0] = '0' + val/100;
arr[1] = '0' + (val%100)/10;
arr[2] = '0' + val%10;
arr[3] = '\0';

printf("%s", arr);

this fails the OPs spec.

I don't understand, in what situation will it fail ?

Why not use the standard library for things it was
made for?

what is the standard library and how to do that ?
thx
 
S

Stefan Höhne

Hi,

Alan said:
Stefan Höhne said:
Hi,

Rick said:
Alan wrote:

I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"

thx

char arr[4];
int val = 123;

arr[0] = '0' + val/100;
arr[1] = '0' + (val%100)/10;
arr[2] = '0' + val%10;
arr[3] = '\0';

printf("%s", arr);

this fails the OPs spec.

I don't understand, in what situation will it fail ?

for val=3. This solution would compute "003", the OP
(=you) explicitely wanted " 3". Of course this is easily
adopted, but needs extra logic.
what is the standard library and how to do that ?
thx

The standard library consists of all those basic
functionality you use everyday: malloc(), printf(),
rand(), sin(), is_alpha(), ...

In this case, sprintf() would be the tool to use, as
Karl suggested.

HTH,
Stefan.
 
J

Jirka Klaue

Stefan Höhne wrote:
....
The standard library consists of all those basic
functionality you use everyday: malloc(), printf(),
rand(), sin(), is_alpha(), ...

I never heard of or used is_alpha().
Well, I never used isalpha as well. :)

Jirka
 
K

Karl Heinz Buchegger

Stefan Höhne said:
In this case, sprintf() would be the tool to use, as
Karl suggested.

Of course not in the way I suggested, the format specifiers
would need some adjustment. But that's any easy thing to do
with sprintf (and there should be some work left for the OP too :)
 
R

Robby C.H.S.

Karl Heinz Buchegger said:
Alan said:
I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"

You don't want to change anything.
You want to convert an integer into its textual representation.

char Buffer[20];
int Number = 123;
sprintf( Buffer, "%d", Number );

Also: When posting to alt.comp.lang.learn.c-c++ always
indicate which language you need a solution for. There
we discuss C and C++. The C++ solution would be completely
different.

Sorry for interrupting.
What is the C++ solution?
 
K

Karl Heinz Buchegger

Robby C.H.S. said:
Karl Heinz Buchegger said:
Alan said:
I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"

You don't want to change anything.
You want to convert an integer into its textual representation.

char Buffer[20];
int Number = 123;
sprintf( Buffer, "%d", Number );

Also: When posting to alt.comp.lang.learn.c-c++ always
indicate which language you need a solution for. There
we discuss C and C++. The C++ solution would be completely
different.

Sorry for interrupting.
What is the C++ solution?

search the group (eg. on google) :)
This question is asked at least twice a day.
Hint: stringstreams
 
B

B. v Ingen Schenau

Robby said:
Karl Heinz Buchegger said:
Alan said:
I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"

You don't want to change anything.
You want to convert an integer into its textual representation.

char Buffer[20];
int Number = 123;
sprintf( Buffer, "%d", Number );

Also: When posting to alt.comp.lang.learn.c-c++ always
indicate which language you need a solution for. There
we discuss C and C++. The C++ solution would be completely
different.

Sorry for interrupting.
What is the C++ solution?

int number = 123;
std::eek:stringstream oss;
oss << std::setw(3) << number;
std::string buffer = oss.str();

Bart v Ingen Schenau
 
M

Martin Ambuhl

Alan said:
I want to change a 3 digits integer to characters, how can i do that?

the 3 digits integer maybe 123, 23 or 3
I want to change the integer to "123", " 23" or " 3"

#include <stdio.h>

char *package(unsigned int num, char *s)
{
if (num > 999)
return 0;
if (sprintf(s, "%3d", num))
return s;
else
return 0;
}

int main(void)
{
int x[3] = { 123, 23, 3 };
char buf[BUFSIZ], *p;
size_t i;
for (i = 0; i < sizeof x / sizeof *x; i++) {
if ((p = package(x, buf)))
printf("%d -> \"%s\"\n", x, p);
else
printf("something went wrong with %d\n", x);
}
return 0;
}


[output]
123 -> "123"
23 -> " 23"
3 -> " 3"

Now give us an exercise from the *second* chapter of your Introduction to C
text.
 
G

Gene Wirchenko

On Wed, 22 Oct 2003 22:38:24 GMT, Martin Ambuhl

[snip]
int main(void)
{
int x[3] = { 123, 23, 3 };
char buf[BUFSIZ], *p;
size_t i;
for (i = 0; i < sizeof x / sizeof *x; i++) {
^^^^^^^^^
What does this mean? I can not help but read it as the size of
an int * *. I would have written "sizeof(int)".

Please explain as I am missing something if this is real code.

[snip]

Sincerely,

Gene Wirchenko
 
M

Mike Wahler

Gene Wirchenko said:
On Wed, 22 Oct 2003 22:38:24 GMT, Martin Ambuhl

[snip]
int main(void)
{
int x[3] = { 123, 23, 3 };
char buf[BUFSIZ], *p;
size_t i;
for (i = 0; i < sizeof x / sizeof *x; i++) {
^^^^^^^^^
What does this mean?

It evaluates to the size of an array element, and
is independent of the array's element type.
I can not help but read it as the size of
an int * *.

No, it's equivalent to 'sizeof(int)'. The type of
'*x' is 'int'.
I would have written "sizeof(int)".

And thus tied it to type 'int'. Martin's code will
work whatever the type of the array element.

-Mike
 
D

Default User

Gene said:
On Wed, 22 Oct 2003 22:38:24 GMT, Martin Ambuhl

[snip]
int main(void)
{
int x[3] = { 123, 23, 3 };
char buf[BUFSIZ], *p;
size_t i;
for (i = 0; i < sizeof x / sizeof *x; i++) {
^^^^^^^^^
What does this mean? I can not help but read it as the size of
an int * *. I would have written "sizeof(int)".

How would you get a pointer to pointer from applying the dereferencing
operator to a pointer?
Please explain as I am missing something if this is real code.

Yes, that divides the size of an entire array but the size of one
element, giving the number of elements in the array. Only works with
real arrays.



Brian Rodenborn
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top