Connecting Tech Pros Worldwide Forums | Help | Site Map

How to convert an integer to ASCII character ?

akarui.tomodachi@gmail.com
Guest
 
Posts: n/a
#1: Feb 19 '06
What is the most easiest way to convert an integer value to ASCII
character format ?
I tried with sprintf(). It works.
Is there any other way to do that ?

Objective::
I like to convert an integer value of 3 and write into a string buffer.

What I did:
.....
.....
char myStr[];
int myInt = 3;
sprintf(myStr, %d,myInt);
.....
.....

Please comment.


serrand
Guest
 
Posts: n/a
#2: Feb 19 '06

re: How to convert an integer to ASCII character ?


akarui.tomodachi@gmail.com wrote:[color=blue]
> What is the most easiest way to convert an integer value to ASCII
> character format ?
> I tried with sprintf(). It works.
> Is there any other way to do that ?
>
> Objective::
> I like to convert an integer value of 3 and write into a string buffer.
>
> What I did:
> ....
> ....
> char myStr[];
> int myInt = 3;
> sprintf(myStr, %d,myInt);
> ....
> ....
>
> Please comment.
>[/color]

#define DIGILEN log10 (MAX_INT) +2

char buf[DIGILEN];
sprintf(buf, "%d", int_var);

Xavier
serrand
Guest
 
Posts: n/a
#3: Feb 19 '06

re: How to convert an integer to ASCII character ?


serrand wrote:[color=blue]
> akarui.tomodachi@gmail.com wrote:
>[color=green]
>> What is the most easiest way to convert an integer value to ASCII
>> character format ?
>> I tried with sprintf(). It works.
>> Is there any other way to do that ?
>>
>> Objective::
>> I like to convert an integer value of 3 and write into a string buffer.
>>
>> What I did:
>> ....
>> ....
>> char myStr[];
>> int myInt = 3;
>> sprintf(myStr, %d,myInt);
>> ....
>> ....
>>
>> Please comment.
>>[/color]
>
> #define DIGILEN log10 (MAX_INT) +2
>
> char buf[DIGILEN];
> sprintf(buf, "%d", int_var);
>
> Xavier[/color]

oops... sorry

#define DIGILEN (int)(log10 (MAX_INT) +3)

Your way seems to be the simpliest...

sprintf is doing the same job as printf : wheras printf outputs in stdin
sprintf outputs in its first argument, which have to be an allocated string

Xavier
OJ
Guest
 
Posts: n/a
#4: Feb 19 '06

re: How to convert an integer to ASCII character ?


Maybe itoa could be used. But it's not a standard function.

shichongdong
Guest
 
Posts: n/a
#5: Feb 19 '06

re: How to convert an integer to ASCII character ?


int i = 3;
char c = i + '0';

Lew Pitcher
Guest
 
Posts: n/a
#6: Feb 19 '06

re: How to convert an integer to ASCII character ?


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

shichongdong wrote:[color=blue]
> int i = 3;
> char c = i + '0';[/color]

???

int i = 300;
char c = i + '0' ; /* nope. not an ascii character */

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFD9+6zagVFX4UWr64RAlFEAJ9CA4LmNOY13Nry6tTDnT 5zrlcO3gCfU/hU
AGWsWWx0Al9HQaZtiP46Gas=
=OS08
-----END PGP SIGNATURE-----
CBFalconer
Guest
 
Posts: n/a
#7: Feb 19 '06

re: How to convert an integer to ASCII character ?


akarui.tomodachi@gmail.com wrote:[color=blue]
>
> What is the most easiest way to convert an integer value to ASCII
> character format ?
> I tried with sprintf(). It works.
> Is there any other way to do that ?
>
> Objective::
> I like to convert an integer value of 3 and write into a string buffer.[/color]

#include <stdio.h>

/* ---------------------- */

static void putdecimal(unsigned int v, char **s) {

if (v / 10) putdecimal(v/10, s);
*(*s)++ = (v % 10) + '0';
**s = '\0';
} /* putdecimal */

/* ---------------------- */

int main(void) {

char a[80];

char *t, *s = a;

t = s; putdecimal( 0, &t); puts(s);
t = s; putdecimal( 1, &t); puts(s);
t = s; putdecimal(-1, &t); puts(s);
t = s; putdecimal( 2, &t); puts(s);
t = s; putdecimal(23, &t); puts(s);
t = s; putdecimal(27, &t); puts(s);
return 0;
} /* main */

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>

Thad Smith
Guest
 
Posts: n/a
#8: Feb 19 '06

re: How to convert an integer to ASCII character ?


akarui.tomodachi@gmail.com wrote:[color=blue]
> What is the most easiest way to convert an integer value to ASCII
> character format ?[/color]

Lew Pitcher wrote:[color=blue]
> shichongdong wrote:[color=green]
>>int i = 3;
>>char c = i + '0';[/color]
>
> ???
>
> int i = 300;
> char c = i + '0' ; /* nope. not an ascii character */[/color]

Interesting. Neither is the earlier code converting 3 guaranteed to
produce ASCII.

--
Thad
Kenny McCormack
Guest
 
Posts: n/a
#9: Feb 19 '06

re: How to convert an integer to ASCII character ?


In article <j5SJf.7348$_D5.487766@news20.bellglobal.com>,
Lew Pitcher <lpitcher@sympatico.ca> wrote:[color=blue]
>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>shichongdong wrote:[color=green]
>> int i = 3;
>> char c = i + '0';[/color]
>
>???
>
> int i = 300;
> char c = i + '0' ; /* nope. not an ascii character */[/color]

The OP was asking how to do it with 3, not 300. You need to keep up.

Martin Jørgensen
Guest
 
Posts: n/a
#10: Feb 19 '06

re: How to convert an integer to ASCII character ?


Lew Pitcher wrote:[color=blue]
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> shichongdong wrote:
>[color=green]
>>int i = 3;
>>char c = i + '0';[/color]
>
>
> ???
>
> int i = 300;
> char c = i + '0' ; /* nope. not an ascii character */[/color]

It works for single digits, right?


Best regards / Med venlig hilsen
Martin Jørgense

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
stathis gotsis
Guest
 
Posts: n/a
#11: Feb 20 '06

re: How to convert an integer to ASCII character ?


"Martin Jørgensen" <unoder.spam@spam.jay.net> wrote in message
news:pkrmc3-l14.ln1@news.tdc.dk...[color=blue]
> Lew Pitcher wrote:[color=green]
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: SHA1
> >
> > shichongdong wrote:
> >[color=darkred]
> >>int i = 3;
> >>char c = i + '0';[/color]
> >
> >
> > ???
> >
> > int i = 300;
> > char c = i + '0' ; /* nope. not an ascii character */[/color]
>
> It works for single digits, right?[/color]

Assuming ASCII it does.


Lew Pitcher
Guest
 
Posts: n/a
#12: Feb 20 '06

re: How to convert an integer to ASCII character ?


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

stathis gotsis wrote:[color=blue]
> "Martin Jørgensen" <unoder.spam@spam.jay.net> wrote in message
> news:pkrmc3-l14.ln1@news.tdc.dk...[color=green]
>> Lew Pitcher wrote:[color=darkred]
>>> -----BEGIN PGP SIGNED MESSAGE-----
>>> Hash: SHA1
>>>
>>> shichongdong wrote:
>>>
>>>> int i = 3;
>>>> char c = i + '0';
>>>
>>> ???
>>>
>>> int i = 300;
>>> char c = i + '0' ; /* nope. not an ascii character */[/color]
>> It works for single digits, right?[/color]
>
> Assuming ASCII it does.[/color]

Assuming any conforming C implementation, it does. The C standard guarantees it.

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFD+QbaagVFX4UWr64RAkS4AJ9H8kT8tck4HFxxhC2f+x mDPRRu5QCgnbor
SX0i8pvlRNTifgvwU0h9Od0=
=YdQ6
-----END PGP SIGNATURE-----
Ben Pfaff
Guest
 
Posts: n/a
#13: Feb 20 '06

re: How to convert an integer to ASCII character ?


Lew Pitcher <lpitcher@sympatico.ca> writes:
[color=blue]
> stathis gotsis wrote:[color=green]
>> "Martin Jxrgensen" <unoder.spam@spam.jay.net> wrote in message
>> news:pkrmc3-l14.ln1@news.tdc.dk...[color=darkred]
>>> Lew Pitcher wrote:
>>>> -----BEGIN PGP SIGNED MESSAGE-----
>>>> Hash: SHA1
>>>>
>>>> shichongdong wrote:
>>>>
>>>>> int i = 3;
>>>>> char c = i + '0';
>>>>
>>>> ???
>>>>
>>>> int i = 300;
>>>> char c = i + '0' ; /* nope. not an ascii character */
>>> It works for single digits, right?[/color]
>>
>> Assuming ASCII it does.[/color]
>
> Assuming any conforming C implementation, it does. The C
> standard guarantees it.[/color]

The C standard guarantees that decimal digits are sequential and
in the proper order. The C standard doesn't guarantee that the
execution character set is ASCII. The OP asked to convert an
integer value to *ASCII* character format specifically.

Here's a portable way to get a single ASCII digit: 48 + num.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Flash Gordon
Guest
 
Posts: n/a
#14: Feb 20 '06

re: How to convert an integer to ASCII character ?


stathis gotsis wrote:[color=blue]
> "Martin Jørgensen" <unoder.spam@spam.jay.net> wrote in message
> news:pkrmc3-l14.ln1@news.tdc.dk...[color=green]
>> Lew Pitcher wrote:[color=darkred]
>>> -----BEGIN PGP SIGNED MESSAGE-----
>>> Hash: SHA1
>>>
>>> shichongdong wrote:
>>>
>>>> int i = 3;
>>>> char c = i + '0';
>>>
>>> ???
>>>
>>> int i = 300;
>>> char c = i + '0' ; /* nope. not an ascii character */[/color]
>> It works for single digits, right?[/color]
>
> Assuming ASCII it does.[/color]

Assuming an implementation that conforms to the C standard it does,
whether it is ASCII or not. It's one of the few things the C standard
guarantees about the execution character set.
--
Flash Gordon
Living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidlines and intro -
http://clc-wiki.net/wiki/Intro_to_clc
stathis gotsis
Guest
 
Posts: n/a
#15: Feb 20 '06

re: How to convert an integer to ASCII character ?


"Flash Gordon" <spam@flash-gordon.me.uk> wrote in message
news:2ndnc3xp47.ln2@news.flash-gordon.me.uk...[color=blue]
> stathis gotsis wrote:[color=green]
> > "Martin Jørgensen" <unoder.spam@spam.jay.net> wrote in message
> > news:pkrmc3-l14.ln1@news.tdc.dk...[color=darkred]
> >> Lew Pitcher wrote:
> >>> -----BEGIN PGP SIGNED MESSAGE-----
> >>> Hash: SHA1
> >>>
> >>> shichongdong wrote:
> >>>
> >>>> int i = 3;
> >>>> char c = i + '0';
> >>>
> >>> ???
> >>>
> >>> int i = 300;
> >>> char c = i + '0' ; /* nope. not an ascii character */
> >> It works for single digits, right?[/color]
> >
> > Assuming ASCII it does.[/color]
>
> Assuming an implementation that conforms to the C standard it does,
> whether it is ASCII or not. It's one of the few things the C standard
> guarantees about the execution character set.[/color]

I was not aware of that, thanks for the correction.


Thad Smith
Guest
 
Posts: n/a
#16: Feb 20 '06

re: How to convert an integer to ASCII character ?


Flash Gordon wrote:[color=blue]
> stathis gotsis wrote:
>[color=green]
>> "Martin Jørgensen" <unoder.spam@spam.jay.net> wrote in message
>> news:pkrmc3-l14.ln1@news.tdc.dk...
>>[color=darkred]
>>> Lew Pitcher wrote:
>>>> shichongdong wrote:
>>>>
>>>>> int i = 3;
>>>>> char c = i + '0';
>>>>
>>>> int i = 300;
>>>> char c = i + '0' ; /* nope. not an ascii character */
>>>
>>> It works for single digits, right?[/color]
>>
>> Assuming ASCII it does.[/color]
>
> Assuming an implementation that conforms to the C standard it does,
> whether it is ASCII or not.[/color]

Check Ben's point elsewhere in the thread. The OP defined "works" as
producing ASCII. While the code in question produces the corresponding
digit character in the execution set, it only produces the correct ASCII
character if the execution set is ASCII.

--
Thad
Dave Thompson
Guest
 
Posts: n/a
#17: Mar 3 '06

re: How to convert an integer to ASCII character ?


On Sun, 19 Feb 2006 01:33:41 +0100, serrand <xavier.serrand@free.fr>
wrote:
[color=blue]
> serrand wrote:[/color]
[color=blue][color=green]
> > #define DIGILEN log10 (MAX_INT) +2
> >
> > char buf[DIGILEN];
> > sprintf(buf, "%d", int_var);[/color][/color]
[color=blue]
> oops... sorry
>
> #define DIGILEN (int)(log10 (MAX_INT) +3)
>[/color]
In C89 an array bound must be a constant expression, and no function
call, even to the standard library, qualifies. In C99 this is still
true for an object with static duration, but if your code snippet is
entirely within a function (not shown) and thus is automatic, this is
legal, though rather inefficient. A C89-legal and (probably) much more
efficient method is to approximate the digits needed for the maximum
value that could be represented in the object size:
sizeof(int)*CHAR_BIT * 10/3 + slop_as_needed
[color=blue]
> Your way seems to be the simpliest...
>
> sprintf is doing the same job as printf : wheras printf outputs in stdin[/color]

stdout. Frequently stdin stdout and stderr are all the/an interactive
terminal or console or window or whatever, but they need not be.
[color=blue]
> sprintf outputs in its first argument, which have to be an allocated string
>[/color]

- David.Thompson1 at worldnet.att.net
Closed Thread


Similar C / C++ bytes