How to convert an integer to ASCII character ?

Discussion in 'C Programming' started by akarui.tomodachi@gmail.com, Feb 19, 2006.

  1. Guest

    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.
    , Feb 19, 2006
    #1
    1. Advertising

  2. serrand Guest

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


    #define DIGILEN log10 (MAX_INT) +2

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

    Xavier
    serrand, Feb 19, 2006
    #2
    1. Advertising

  3. serrand Guest

    serrand wrote:
    > wrote:
    >
    >> 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.
    >>

    >
    > #define DIGILEN log10 (MAX_INT) +2
    >
    > char buf[DIGILEN];
    > sprintf(buf, "%d", int_var);
    >
    > Xavier


    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
    serrand, Feb 19, 2006
    #3
  4. OJ Guest

    Maybe itoa could be used. But it's not a standard function.
    OJ, Feb 19, 2006
    #4
  5. shichongdong Guest

    int i = 3;
    char c = i + '0';
    shichongdong, Feb 19, 2006
    #5
  6. Lew Pitcher Guest

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

    - --
    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+6zagVFX4UWr64RAlFEAJ9CA4LmNOY13Nry6tTDnT5zrlcO3gCfU/hU
    AGWsWWx0Al9HQaZtiP46Gas=
    =OS08
    -----END PGP SIGNATURE-----
    Lew Pitcher, Feb 19, 2006
    #6
  7. CBFalconer Guest

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


    #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/>
    CBFalconer, Feb 19, 2006
    #7
  8. Thad Smith Guest

    wrote:
    > What is the most easiest way to convert an integer value to ASCII
    > character format ?


    Lew Pitcher wrote:
    > shichongdong wrote:
    >>int i = 3;
    >>char c = i + '0';

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


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

    --
    Thad
    Thad Smith, Feb 19, 2006
    #8
  9. In article <j5SJf.7348$>,
    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 */


    The OP was asking how to do it with 3, not 300. You need to keep up.
    Kenny McCormack, Feb 19, 2006
    #9
  10. 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?


    Best regards / Med venlig hilsen
    Martin Jørgense

    --
    ---------------------------------------------------------------------------
    Home of Martin Jørgensen - http://www.martinjoergensen.dk
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 19, 2006
    #10
  11. "Martin Jørgensen" <> wrote in message
    news:p...
    > 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?


    Assuming ASCII it does.
    stathis gotsis, Feb 19, 2006
    #11
  12. Lew Pitcher Guest

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

    stathis gotsis wrote:
    > "Martin Jørgensen" <> wrote in message
    > news:p...
    >> 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?

    >
    > Assuming ASCII it does.


    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+xmDPRRu5QCgnbor
    SX0i8pvlRNTifgvwU0h9Od0=
    =YdQ6
    -----END PGP SIGNATURE-----
    Lew Pitcher, Feb 20, 2006
    #12
  13. Ben Pfaff Guest

    Lew Pitcher <> writes:

    > stathis gotsis wrote:
    >> "Martin Jxrgensen" <> wrote in message
    >> news:p...
    >>> 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?

    >>
    >> Assuming ASCII it does.

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


    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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \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\
    );}return 0;}
    Ben Pfaff, Feb 20, 2006
    #13
  14. Flash Gordon Guest

    stathis gotsis wrote:
    > "Martin Jørgensen" <> wrote in message
    > news:p...
    >> 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?

    >
    > Assuming ASCII it does.


    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
    Flash Gordon, Feb 20, 2006
    #14
  15. "Flash Gordon" <> wrote in message
    news:-gordon.me.uk...
    > stathis gotsis wrote:
    > > "Martin Jørgensen" <> wrote in message
    > > news:p...
    > >> 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?

    > >
    > > Assuming ASCII it does.

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


    I was not aware of that, thanks for the correction.
    stathis gotsis, Feb 20, 2006
    #15
  16. Thad Smith Guest

    Flash Gordon wrote:
    > stathis gotsis wrote:
    >
    >> "Martin Jørgensen" <> wrote in message
    >> news:p...
    >>
    >>> 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?

    >>
    >> Assuming ASCII it does.

    >
    > Assuming an implementation that conforms to the C standard it does,
    > whether it is ASCII or not.


    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
    Thad Smith, Feb 20, 2006
    #16
  17. On Sun, 19 Feb 2006 01:33:41 +0100, serrand <>
    wrote:

    > serrand wrote:


    > > #define DIGILEN log10 (MAX_INT) +2
    > >
    > > char buf[DIGILEN];
    > > sprintf(buf, "%d", int_var);


    > oops... sorry
    >
    > #define DIGILEN (int)(log10 (MAX_INT) +3)
    >

    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

    > Your way seems to be the simpliest...
    >
    > sprintf is doing the same job as printf : wheras printf outputs in stdin


    stdout. Frequently stdin stdout and stderr are all the/an interactive
    terminal or console or window or whatever, but they need not be.

    > sprintf outputs in its first argument, which have to be an allocated string
    >


    - David.Thompson1 at worldnet.att.net
    Dave Thompson, Mar 3, 2006
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xianwen Chen

    Convert an integer value to ASCII character

    Xianwen Chen, Feb 13, 2011, in forum: C Programming
    Replies:
    9
    Views:
    4,678
    alphareaper
    Oct 31, 2012
  2. John Gregory
    Replies:
    0
    Views:
    278
    John Gregory
    Jul 5, 2009
  3. Alextophi
    Replies:
    8
    Views:
    482
    Alan J. Flavell
    Dec 30, 2005
  4. bruce
    Replies:
    38
    Views:
    255
    Mark Lawrence
    Nov 1, 2013
  5. MRAB
    Replies:
    0
    Views:
    85
Loading...

Share This Page