Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > integer to characters

Reply
Thread Tools

integer to characters

 
 
Alan
Guest
Posts: n/a
 
      10-22-2003
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




 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      10-22-2003


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"
>


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.

--
Karl Heinz Buchegger

 
Reply With Quote
 
 
 
 
Alan
Guest
Posts: n/a
 
      10-22-2003

"Karl Heinz Buchegger" <> ???
news: ???...
>
> 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"
> >

>
> 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


 
Reply With Quote
 
Rick
Guest
Posts: n/a
 
      10-22-2003
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);


Rick

 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      10-22-2003


Alan wrote:
>
> "Karl Heinz Buchegger" <> ???
> news: ???...
> >
> > 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"
> > >

> >
> > 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

--
Karl Heinz Buchegger

 
Reply With Quote
 
Robert W Hand
Guest
Posts: n/a
 
      10-22-2003
On Wed, 22 Oct 2003 19:12:21 +1000, Rick <rrquick@nospam-com> wrote:

>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);


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

Best wishes,

Bob
 
Reply With Quote
 
Stefan Höhne
Guest
Posts: n/a
 
      10-22-2003
Hi,


"Rick" <rrquick@nospam-com> wrote in message
news:3f9647af$...
> 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.

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

Regards,
Stefan.


 
Reply With Quote
 
Alan
Guest
Posts: n/a
 
      10-22-2003

"Stefan Höhne" <do_not_use_this_adress@127.0.0.1> ¦b¶l¥ó
news:bn5jog$2vk$ ¤¤¼¶¼g...
> Hi,
>
> "Rick" <rrquick@nospam-com> wrote in message
> news:3f9647af$...
> > 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 ?


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


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



 
Reply With Quote
 
Stefan Höhne
Guest
Posts: n/a
 
      10-22-2003
Hi,

"Alan" <> wrote in message
news:bn5lpq$qpq$...
>
> "Stefan Höhne" <do_not_use_this_adress@127.0.0.1> ¦b¶l¥ó
> news:bn5jog$2vk$ ¤¤¼¶¼g...
> > Hi,
> >
> > "Rick" <rrquick@nospam-com> wrote in message
> > news:3f9647af$...
> > > 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.

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

>
> 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.


 
Reply With Quote
 
Jirka Klaue
Guest
Posts: n/a
 
      10-22-2003
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I add an Integer to another Integer? Sebastian Stelzer Java 6 04-07-2010 07:03 PM
CType(x,Integer) vs. Integer.Parse(x) =?Utf-8?B?Sm9l?= ASP .Net 7 02-07-2006 02:30 AM
how do I make Class.forName("Integer") returning java.lang.Integer? Johannes Zellner Java 22 12-19-2005 11:22 AM
How do I add an Integer to another Integer? Sebastian Stelzer Java 2 10-15-2004 01:17 PM
No Math.min(Integer, Integer)? =?ISO-8859-1?Q?Thomas_Gagn=E9?= Java 0 07-29-2003 07:46 PM



Advertisments