How to convert string to integer in MASM
I want to simply get the user to input a number. I can get input (I think), but this is a string, and I need to conve开发者_如何学Pythonrt it to a number (DWORD). I couldn't find anything that worked in MASM. I tried the C functions strtol and atoi, but it couldn't find them. Is there some function that works in MASM? Or do I have to write my own conversion?
I tried several include files, but I couldn't get the C functions:
windows.inc kernel32.inc user32.inc msvcrt.incIts over 20 years since I used MASM so I'm a bit rusty. The algorithm is pretty straightforward though.
- Assuming your string is in ASCII
- Start at the end of the string
- You will need to split each character off the string and subtract 30H from it to give the digit
- store that digit in a register
- calculate your next power of ten
- Each time you move one character left and get that digit multiply by the next power of ten
- add to accumulator
E.g for an integer
31H 32H 33H
31h-30H = 1H 32H-30H = 2H * 10 33H-30H = 3H * 10 * 10
Bingo!
If the number represents a double you will need to cope with in a similar manner.
I did atoi PROTO C strptr:DWORD
and then I was able to call atoi
.
精彩评论