Results 1 to 11 of 11

Thread: [RESOLVED] int to ASCII..

  1. #1

    Thread Starter
    Junior Member xuralarux's Avatar
    Join Date
    Oct 2001
    Location
    I'm somewhere where I don't know where I am!
    Posts
    20

    [RESOLVED] int to ASCII..

    How would I go about converting an integer to it's equivalent ASCII value?
    Last edited by xuralarux; Feb 13th, 2003 at 08:24 PM.
    Before you say anything about this post...you're welcome.

    "I love animals... I eat them and wear their skin."

    "Why do those who know the least, know it the loudest?"

    "You live and learn. Or you don't live long."



    Which Colossal Death Robot Are You?

  2. #2
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    itoa() in stdlib.h ?

  3. #3
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    If you just want to convert a single digit (0-9), do this

    char character=(char)(digit+48);

  4. #4

    Thread Starter
    Junior Member xuralarux's Avatar
    Join Date
    Oct 2001
    Location
    I'm somewhere where I don't know where I am!
    Posts
    20
    Does that return the string representation of a number (ie "65"), or for a value like 65 return the equivalent ASCII character, "A"?

  5. #5
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    All characters are represented as numbers in a computer -

    char can be treated as a number (unsigned: 1-255) (signed -127 -127)or whatever the ASCII designation for the char is. Internally it is still a number.

    Code:
    char s;
    for(s=65;s<91;s++) printf(" %c \n",s);
    And you can convert any integer that is <255 to char. Many character functions like getc() actually use integers. Not char.
    char and int are interchangeable for a lot of functions.

  6. #6
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Originally posted by xuralarux
    Does that return the string representation of a number (ie "65"), or for a value like 65 return the equivalent ASCII character, "A"?
    I think someone else had shown this piece of code here before.

    cout<<(int)('A')<<endl;//65

    cout<<(char)(65)<<endl;//'A' character

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    [edit]Forget what I said here. I was tired.[/edit]

    For a long number, it's sprintf (_itoa is an MS extension).
    Last edited by CornedBee; Feb 13th, 2003 at 03:38 AM.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Junior Member xuralarux's Avatar
    Join Date
    Oct 2001
    Location
    I'm somewhere where I don't know where I am!
    Posts
    20
    Originally posted by transcendental
    I think someone else had shown this piece of code here before.

    cout<<(int)('A')<<endl;//65

    cout<<(char)(65)<<endl;//'A' character
    Ok, so really as long as you can get an int, you can just use a (char) cast to get the equivalent ASCII character... And here I was getting the idea that doing number <-> ASCII stuff was easier in VB! Now that I'm kinda getting the hang of C++, I don't think I wanna go back to Micro$haft stuff! Well, mebbe if a juicy position as a SQL DB admin popped up, but other than that...

  9. #9
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Originally posted by CornedBee
    BTW it's not + 48 but either + 0x48 or + 65 or (best) + 'A'.

    For a long number, it's sprintf (_itoa is an MS extension).
    BTW it's not + 48 but either + 0x41 or + 65 or (best) + 'A'.

    Anyway I'm really referring to 48. 48 is the ascii no for '0' since the thread title is 'int to ASCII'.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Oh yeah, stupid me.


    It was 2 AM...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11

    Thread Starter
    Junior Member xuralarux's Avatar
    Join Date
    Oct 2001
    Location
    I'm somewhere where I don't know where I am!
    Posts
    20
    Thanks for the help all, got the program done.
    Before you say anything about this post...you're welcome.

    "I love animals... I eat them and wear their skin."

    "Why do those who know the least, know it the loudest?"

    "You live and learn. Or you don't live long."



    Which Colossal Death Robot Are You?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width