×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# ASCII to decimals

## ASCII to decimals

(OP)
Hi,
I am to write a program that can perform addition, subtraction, multiplication and division for 2 32-bit numbers.

It should ask user to input num1, num2 and the desired operation. Then it will calculate and print out the result.

The thing is, when I take user input, it's just a string of number (or ASCII digits). How can I actually perform a convertion from ASCII to number, calculate and convert back to ASCII for displaying ?

Thank you very much..

### RE: ASCII to decimals

First: know that the ascii for numeric digits are from the range 48(0) to 57(9).

Second: know that those numbers, in hex, are:
30h to 39h

Third: know that you can extract the least significant (rightmost) hex digit by:
and (something), 0fh

Fourth: know that a number such as '3192' is stored in the following fashion:
33h 31h 39h 32h <some value that isn't between 30h and 39h>

Fifth: so you set some accumulator variable (not necessarily ax...) to 0.  Create a loop: multiply accumulator by 10, 'AND' the current byte with 0fh, add to accumulator.  If next byte isn't between 30 and 39h, stop and return contents of accumulator variable.  Otherwise, keep looping.

Sixth: in the example above, first time through loop, acc is 0, then you add 3.  2x through loop, you are looking at the next byte, acc is 3, multiply by 10, is 30, add 1, to get 31.  3x through loop, acc is 31, multiply by 10, is 310, add 9, equals 319.  4x through loop, acc is 319, multiply by 10, is 3190, add 2, is 3192.  You notice the next byte is a delimiting character, so you stop and return the value to whoever called you.

Last: That's assuming you have a delimiting character.  Otherwise, if you know the length of the string, put a null into that position so that you have a delimiting character and your function will not fail.

"Information has a tendency to be free.  Which means someone will always tell you something you don't want to know."

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!