Convert an integer value to ASCII character

Discussion in 'C Programming' started by Xianwen Chen, Feb 13, 2011.

  1. Xianwen Chen

    Xianwen Chen Guest

    Hi there,

    I'm trying to convert an integer valune to ASCII character. I have
    found that I can print an integer value to a character by
    printf("%c", intA);
    What I am looking for is something like
    charA = somefunction(intA)

    Some hints, please?

    Regards,

    Xianwen
    Xianwen Chen, Feb 13, 2011
    #1
    1. Advertising

  2. Xianwen Chen

    Stefan Ram Guest

    Xianwen Chen <> writes:
    >I'm trying to convert an integer valune to ASCII character.


    This is not specific enough. Do you want to convert
    a value of type »int« into a value of type »char«?

    Or, otherwise, what else?

    >charA = somefunction(intA)


    What about

    char somefunction( int const i ){ return i; }
    int main( void ){ return somefunction( 0 ); }

    ?

    Do you refer to ASCII 1963 or ASCII 1968, and what does
    ASCII has to do with converting from char to int?
    Stefan Ram, Feb 13, 2011
    #2
    1. Advertising

  3. Xianwen Chen

    Dr Nick Guest

    Xianwen Chen <> writes:

    > Hi there,
    >
    > I'm trying to convert an integer valune to ASCII character. I have
    > found that I can print an integer value to a character by
    > printf("%c", intA);
    > What I am looking for is something like
    > charA = somefunction(intA)
    >
    > Some hints, please?


    If you have the right value in there (so the printf is working) you just
    need to assign the value.

    charA = intA will do the job for you.

    Note that there's no actual guarantee that C uses ASCII (and I've
    programmed in C on machines that don't) but if you're prepared to live
    with this while you're learning it will work.

    The reason this works is that in C, characters are just small integers
    holding the code (not necessarily, but often ASCII) for the character.
    --
    Online waterways route planner | http://canalplan.eu
    Plan trips, see photos, check facilities | http://canalplan.org.uk
    Dr Nick, Feb 13, 2011
    #3
  4. Xianwen Chen

    Stefan Ram Guest

    -berlin.de (Stefan Ram) writes:
    >Do you refer to ASCII 1963 or ASCII 1968, and what does
    >ASCII has to do with converting from char to int?


    (this should have been: »... from int to char?«)

    I have thought again about this:

    First, I assume an execution environment whose execution
    character (5.2.1p1) set is ASCII:

    6.4.4.4p10: »The value of an integer character constant
    containing a single character that maps to a single-byte
    execution character is the numerical value of the
    representation of the mapped character interpreted as an
    integer.«

    7.19.2p1 ... p2: »Input and output (...) are mapped into
    logical data streams. A text stream is an ordered sequence
    of characters (...)«

    7.19.7.3p2: »The fputc function writes the character
    specified (...) to the output stream pointed to by stream«.

    So, when fputc is used to write to a text stream on an
    execution environment whose execution character (5.2.1p1)
    set is ASCII, the value written should be converted to ASCII
    using an implementation-defined mapping (that should be
    ASCII in this case).

    On a system without another execution character set than
    ASCII, one can implement the mapping from the execution
    character set to ASCII oneself by a table and then write the
    ASCII characters into a binary stream out as a sequence of
    octets.
    Stefan Ram, Feb 13, 2011
    #4
  5. Xianwen Chen

    Geoff Guest

    On Sun, 13 Feb 2011 08:13:33 -0800 (PST), Xianwen Chen
    <> wrote:

    >Hi there,
    >
    >I'm trying to convert an integer valune to ASCII character. I have
    >found that I can print an integer value to a character by
    > printf("%c", intA);
    >What I am looking for is something like
    > charA = somefunction(intA)
    >
    >Some hints, please?
    >
    >Regards,
    >
    >Xianwen


    This sounds like a homework problem.

    It seems that what you are looking for is the reverse of the atoi()
    function found in stdlib.h, however atoi() converts an ASCII string
    into an integer, not just a character. Standard C doesn't provide the
    reverse for some reason. One would have thought it would have been
    included for orthogonality sake. Microsoft provides the _itoa()
    function in their libraries but this is an extension.

    K&R implemented itoa() in both the 1st and 2nd edition of "The C
    Programming Language" but they were both flawed in some respects.

    The standard-conformant method these days is to use sprintf().
    Geoff, Feb 13, 2011
    #5
  6. Xianwen Chen

    Stefan Ram Guest

    -berlin.de (Stefan Ram) writes:
    >6.4.4.4p10: »The value of an integer character constant
    >containing a single character that maps to a single-byte
    >execution character is the numerical value of the
    >representation of the mapped character interpreted as an
    >integer.«


    6.4.4.4p2: »An integer character constant is a sequence of
    one or more multibyte characters enclosed in single-quotes,
    as in 'x'.«

    Thus, 'x' is an integer character constant. It contains a
    single character.

    I guess, it maps to the single-byte execution character »x«.
    But why?

    5.1.1.2p1,5. »Each source character set member and escape
    sequence in character constants and string literals is
    converted to the corresponding member of the execution
    character set«

    But where is this correspondence being defined? This seems
    to be implied by the text, but not explictly specified.

    So, it seems to be safe to assume that 'x' is being mapped
    to the execution character »x«. Actually, to the numerical
    value of its representation.

    In ASCII 1968 this representation is: 1111000, the numerical
    value of which is 120.

    Thus, the value of 'x' is 120 on a C implementation with
    ASCII as its execution character set.

    >7.19.7.3p2: »The fputc function writes the character
    >specified (...) to the output stream pointed to by stream«.


    In »fputc( 'x', textstream )«, fputc or the text stream do
    not have to do any more work to convert to the execution
    character set, because 'x' already is the numerical value of
    the proper representation.

    The first parameter of fputc is int, so it can accept int or
    char as argument type and the question whether int or char
    is used does not have to do anything with the question
    whether the numerical value represents a character or not.
    Stefan Ram, Feb 13, 2011
    #6
  7. On Feb 13, 10:13 am, Xianwen Chen <> wrote:
    > Hi there,
    >
    > I'm trying to convert an integer valune to ASCII character. I have
    > found that I can print an integer value to a character by
    >    printf("%c", intA);
    > What I am looking for is something like
    >    charA = somefunction(intA)
    >
    > Some hints, please?
    >
    > Regards,
    >
    > Xianwen


    Assuming that by "integer value" you mean an int in the range [0,9],
    then you can convert it to the ASCII code by adding 0x30, as any
    ASCII table will tell you. It isn't clear that you mean this, but
    I can't think of anything else you might mean.

    If the execution character set is ascii already, you could do
    something like this:

    int i = 5;
    char c;
    switch(i) {
    case 0: c = '0';
    case 1: c = '1';
    case 2: c = '2';
    case 3: c = '3';
    case 4: c = '4';
    case 5: c = '5';
    case 6: c = '6';
    case 7: c = '7';
    case 8: c = '8';
    case 9: c = '9';
    default: exit(42);
    }
    printf("%c\n", c);

    But they may laugh at you.
    luser- -droog, Feb 14, 2011
    #7
  8. luser- -droog <> writes:

    > On Feb 13, 10:13 am, Xianwen Chen <> wrote:
    >> Hi there,
    >>
    >> I'm trying to convert an integer valune to ASCII character. I have
    >> found that I can print an integer value to a character by
    >>    printf("%c", intA);
    >> What I am looking for is something like
    >>    charA = somefunction(intA)
    >>
    >> Some hints, please?

    <snip>
    > Assuming that by "integer value" you mean an int in the range [0,9],
    > then you can convert it to the ASCII code by adding 0x30, as any
    > ASCII table will tell you. It isn't clear that you mean this, but
    > I can't think of anything else you might mean.


    I suspect the OP has not yet fully understood that char values are
    integer values and that (apart from maybe doing a range check)
    somefunction(x) is the identity function in C. But you are right that
    it's not entirely clear.

    > If the execution character set is ascii already, you could do
    > something like this:
    >
    > int i = 5;
    > char c;
    > switch(i) {
    > case 0: c = '0';
    > case 1: c = '1';
    > case 2: c = '2';
    > case 3: c = '3';
    > case 4: c = '4';
    > case 5: c = '5';
    > case 6: c = '6';
    > case 7: c = '7';
    > case 8: c = '8';
    > case 9: c = '9';
    > default: exit(42);
    > }
    > printf("%c\n", c);
    >
    > But they may laugh at you.


    I am filled with questions... Who is 'they'? Is the absence of 'break'
    a deliberate homework sabotaging ploy? Do you know that the decimal
    digits are guaranteed to be consecutive?

    --
    Ben.
    Ben Bacarisse, Feb 14, 2011
    #8
  9. On Feb 14, 5:35 am, Ben Bacarisse <> wrote:
    > luser- -droog <> writes:
    > > On Feb 13, 10:13 am, Xianwen Chen <> wrote:
    > >> Hi there,

    >
    > >> I'm trying to convert an integer valune to ASCII character. I have
    > >> found that I can print an integer value to a character by
    > >>    printf("%c", intA);
    > >> What I am looking for is something like
    > >>    charA = somefunction(intA)

    >
    > >> Some hints, please?

    > <snip>
    > > Assuming that by "integer value" you mean an int in the range [0,9],
    > > then you can convert it to the ASCII code by adding 0x30, as any
    > > ASCII table will tell you. It isn't clear that you mean this, but
    > > I can't think of anything else you might mean.

    >
    > I suspect the OP has not yet fully understood that char values are
    > integer values and that (apart from maybe doing a range check)
    > somefunction(x) is the identity function in C.  But you are right that
    > it's not entirely clear.
    >
    >
    >
    > > If the execution character set is ascii already, you could do
    > > something like this:

    >
    > > int i = 5;
    > > char c;
    > > switch(i) {
    > >     case 0: c = '0';
    > >     case 1: c = '1';
    > >     case 2: c = '2';
    > >     case 3: c = '3';
    > >     case 4: c = '4';
    > >     case 5: c = '5';
    > >     case 6: c = '6';
    > >     case 7: c = '7';
    > >     case 8: c = '8';
    > >     case 9: c = '9';
    > >     default: exit(42);
    > > }
    > > printf("%c\n", c);

    >
    > > But they may laugh at you.

    >
    > I am filled with questions...  Who is 'they'?  


    Anyone aware that doing a switch for this task is silly.

    > Is the absence of 'break'
    > a deliberate homework sabotaging ploy?


    No, but I wish I had. It was just an oversight.

    > Do you know that the decimal
    > digits are guaranteed to be consecutive?


    I did know that but somehow forgot while writing the message.
    But since the OP asked merely for hints, I didn't feel any
    great need to offer a tested solution.
    luser- -droog, Feb 14, 2011
    #9
  10. Xianwen Chen

    alphareaper

    Joined:
    Oct 31, 2012
    Messages:
    1


    my dear friends, you all are certainly using wrong approach to solve the problem.the guy just simply wants to convert int into ascii. To do so, the program i write below. it is tested and working fine.


    #include<stdio.h>
    #include<string.h>
    #include<conio.h>

    char data[1000]= {' '}; /*thing in the bracket is optional*/
    char data1[1000]={' '};
    int val, a;
    char varray [9];

    void binary (int digit)
    {
    if(digit==0)
    val=48;
    if(digit==1)
    val=49;
    if(digit==2)
    val=50;
    if(digit==3)
    val=51;
    if(digit==4)
    val=52;
    if(digit==5)
    val=53;
    if(digit==6)
    val=54;
    if(digit==7)
    val=55;
    if(digit==8)
    val=56;
    if(digit==9)
    val=57;
    a=0;

    while(val!=0)
    {
    if(val%2==0)
    {
    varray[a]= '0';
    }

    else
    varray[a]='1';
    val=val/2;
    a++;
    }


    while(a!=7)
    {
    varray[a]='0';
    a++;
    }


    varray [8] = NULL;
    strrev (varray);
    strcpy (data1,varray);
    strcat (data1,data);
    strcpy (data,data1);
    }


    void main()
    {
    int num;
    clrscr();
    printf("enter number\n");
    scanf("%d",&num);
    if(num==0)
    binary(0);
    else
    while(num>0)
    {
    binary(num%10);
    num=num/10;
    }
    puts(data);
    getch();

    }

    i checked my code and its working good.copy this code in any c++ compiler and you are good to go.thanks.
    alphareaper, Oct 31, 2012
    #10
    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. Replies:
    16
    Views:
    2,274
    Dave Thompson
    Mar 3, 2006
  2. John Gregory
    Replies:
    0
    Views:
    278
    John Gregory
    Jul 5, 2009
  3. Alextophi
    Replies:
    8
    Views:
    481
    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