Anybody please help me how to convert string to integer..

example, get string by using "call ReadString" then input "123" then i want to convert it into 123 as an integer.  How can i do this?

Thanks in advanced!
Posted on 2006-08-20 04:01:38 by boonmai
You want to convert an ascii string-based integer to a dword integer?
Check the masm32lib for atodw :)
Posted on 2006-08-20 04:16:24 by Homer
As Homer suggested, check out the masm32lib for atodw, and many other useful 'tools' to help you learn (the lib is a fantasic resource to understand how assembly is applied).  My additional comment is that I hope you are learning from all this.  In you past post looking for a conversion form IP# string to 32bit DWORD, you got your answer but I didn't see much in that thread that suggested you're learning from all this. 

We are here to help you learn so please ask questions to better your understanding of how assembly is applied.  If your simply fishing for your needs to be done by others, you will find this pond will dry up fast.  (My gut feel is you are genuinely looking to learn from this ~ but somehow I doubt you understand how the past IP solution works ;)  ).  So that said, ask questions so we can get to know you and your intentions and we will be happy to help with all we can offer.

Happy programming.
Posted on 2006-08-21 13:42:58 by NaN
i'll try to redeem myself , though it seemed to me at first that he was learning
winsock and didnt find any code for that conversion...
every positive integer can be represented in form of:
Nn*Bm + Nn-1*Bm-1 + ... + N1*B1 + N0


for example 123 can be written as:

so to convert str to num you have to put every digit in place

example 123456
(SEE: "multiply and add" lines in source)

to understand how ascii codes for numbers are sorted use windows charmap program
: start->run->charmap

.model flat,stdcall
option casemap:none
include windows.inc
include kernel32.inc
includelib kernel32.lib

ReadString proto lpBuffer:PTR BYTE,dwBuffLen:DWORD

buff db 100h dup(?)



; input number ("123" for example) and
; hit Enter for function to return
invoke ReadString,addr buff,sizeof buff
; here buff will contain the number you entered
int 3 ; the best way to learn whats going on is
      ; to watch it in debugger (ollydbg)
; convert string to dword

lea esi,buff ; esi will have address of the buffer
xor eax,eax; we will build up the number in eax


movzx ebx,byte ptr; load byte from esi, zero extending byte to dword
inc esi; increase esi by one

test ebx,ebx ; is ebx null?
jz @@done

; before : ebx can contain ascii codes for 0,1,2,3,4,5,6,7,8,9
;          which are : 30h,31h,32h,33h,...,39h
;          or in decimal : 48,49,50,...,57

sub ebx,30h; 30h is ascii code for '0' and we want the relative
          ; distance from base (from 30h)

; after : ebx will contain 0,1,2,3,4,...,9 decimal (or hex, its the same)

; multiply and add

mov ecx,10 ; NUMBER_BASE is 10 (decimal)
mul ecx;  edx::eax = eax * ecx
add eax,ebx

jmp again ; do the next digit

; eax = number

; notes:
; - if you put this code in a proc remember to preserve ebx and esi
; - if you want to do binary to dword conversion all you need to
;  change is NUMBER_BASE = 2
; - for hex its a little different because there is a "gap" in
;  between '9' and 'A'
invoke ExitProcess,eax


ReadString proc lpBuffer:PTR BYTE,dwBuffLen:DWORD
LOCAL hStdIn,nNumBytesRead
invoke GetStdHandle,STD_INPUT_HANDLE; get input handle
mov hStdIn,eax
.if lpBuffer!=0
;  read from console
invoke ReadFile,hStdIn,lpBuffer,dwBuffLen,addr nNumBytesRead,0
mov eax,nNumBytesRead
mov edx,lpBuffer
sub eax,2
and byte ptr ,0; put null terminator on CR,LF
ReadString endp

end start

Posted on 2006-08-21 19:32:35 by drizz