Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASCII to decimals

Status
Not open for further replies.

dukely

Technical User
Sep 18, 2002
6
0
0
US
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..
 
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. &quot;Information has a tendency to be free. Which means someone will always tell you something you don't want to know.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top