Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Vibhor16

  • 4 years ago

I want to convert a char to int like '7' to 7 (in C++) but every time I try it gives me its ASCII value. Basically I just want to multiply two characters like '7' * '7' = 49.

  • This Question is Closed
  1. jagatuba
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    I solved a similar problem to this in Java by subtracting the appropriate amount from the ascii value. So numerals 0-9 ascii values are 30-39, so simply subtract 30 from your ascii value and assign it to the variable that you want. Pseudo code: Print "Enter a number: " Input numString Parse numString to numAscii numInt=numAscii-30 numInt=numInt*7 Print numInt

  2. jagatuba
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    You could also mod 10 the ascii value.

  3. agdgdgdgwngo
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    for the '0' to '9' ASCII characters, just subtract '0' (the character) from it, because character literals in C (and i think C++ as well) are just integer values of their ASCII encoding. for example: http://ideone.com/noXGk

  4. jagatuba
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Thanks agdg. I knew you'd pipe in with something useful.

  5. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy