How can i convert String to its number equivalent in Win32Asm; ie, i get input from the user via String Parameters and to extract that parameter into usable number and vice versa; if this is a _wsprintf function, can you please show me how to do it? i personally dont know how to deal with _wsprintf function :o example: "12345" to 12345 "1234h" to 4660 etch, tnx -Alex :)
Posted on 2001-02-20 23:02:00 by gwapo
From you message it doesn't appear that wsprintf will help - that function can convert numbers and put them in a string, but it doesn't do the opposite. To convert a string to a number: 1.Make sure the text is all numbers (or 0-9, A-F for hex) 2.Grab each character and multiple by the base (ex. 10 for decimal) 3.Stop at the end of the string, or character out-of-range. Optionally, you could check for a minus sign at the first character position, and negate the result of the above algorithm. Also, you could check for the 'h' at the end of the number string and set the base, or '0x', or whatever to determine if the input is hex. Oh, there is a function in M32LIB :P It's for decimal. See \MASM32\M32LIB\A2DW.ASM (ASCII to DWORD) bitRAKE
Posted on 2001-02-21 00:02:00 by bitRAKE
The MASM32.LIB has function which will allow you to do just that.

invoke atodw,ADDR MyDecimalString
invoke htodw,ADDR MyHexString
Both return the value in eax Just scan your string for an "h" at the end, and pick the function based on that. Will work for up to DWORD values.
Posted on 2001-02-21 00:03:00 by Q
Here is my A2DW.ASM It assumes the string has been checked, or is known to be digits 0-9, and that it's not to many digits. Please, critique! Can we have a forum for just developing the M32LIB? Or maybe LIBs development in general? bitRAKE

	;----------------------------------------
	; Convert decimal string into dword value
	; return value in eax
	;----------------------------------------

	.code

a2dw	PROC USES edi,
	String:PTR BYTE

	.data

ten	dd	10

	.code

	mov	edi, String

	xor	ecx, ecx			;temp holder for current digit
	xor	eax, eax			;result
lp:
	mov	cl, BYTE PTR 	;get a digit
	inc	edi				;move pointer to next digit
	sub	cl, "0"			;convert ASCII digit to binary
	js	bye				;less than an ASCII zero is a bad thing, or end of string
	mul	DWORD PTR 		;previous digits are more significant
	add	eax, ecx			;add in this digit
	jmp	lp				;keep going till non-digit
bye:
	ret

a2dw	ENDP
Posted on 2001-02-21 01:03:00 by bitRAKE
tnx, that helps me a lot
Posted on 2001-02-22 03:08:00 by gwapo
Then there is the more general approach:

; Routine for converting an ASCII string to a 64 bit integer in any base.
; X-Calibre, Diamond Crew 2000

ParseRadixUnsigned	PROC
; Input:	Pointer to zero-terminated string in ESI, radix in EDI
; Output:	Parsed number in EDX::EAX
; Uses:		EAX, EBX, ECX, EDX, ESI, EDI

	xor		ebx, ebx

	; result in EDX::EAX
	xor		eax, eax
	xor		edx, edx

	mov		al, 
	inc		esi
	test	eax, eax
	jz		@@endOfParsing

	sub		eax, 30h
	.IF eax > 9
		sub		eax, 7
	.ENDIF

	mov		bl, 

@@smallParseLoop:
	; ASCII to number conversion
	sub		ebx, 30h
	inc		esi
	mul		edi
	.IF ebx > 9
		sub		ebx, 7
	.ENDIF
	add		eax, ebx
	mov		bl, 
	jc		@@carry
	test	ebx, ebx
	jnz		@@smallParseLoop
	
	ret

@@carry:
	inc		edx
	test	ebx, ebx
	jz		@@endOfParsing

@@bigParseLoop:
	; ASCII to number conversion
	mov		ecx, eax
	mov		eax, edx
	sub		ebx, 30h
	inc		esi
	mul		edi
	xchg	eax, ecx
	mul		edi
	.IF ebx > 9
		sub		ebx, 7
	.ENDIF
	add		eax, ebx
	mov		bl, 
	adc		edx, ecx

	test	ebx, ebx
	jnz		@@bigParseLoop

@@endOfParsing:
	ret
ParseRadixUnsigned	ENDP

ParseRadixSigned	PROC
; Input:	Pointer to zero-terminated string in ESI, radix in EDI
; Output:	Parsed number in EDX::EAX
; Uses:		EAX, EBX, ECX, EDX, ESI, EDI

	.code
	; If string does not start with a '-', consider it positive
	cmp		byte ptr , '-'
	jne		ParseRadixUnsigned

	; Number is negative, first parse the absolute value
	inc		esi

	call	ParseRadixUnsigned

	; Now negate the absolute value to get the negative result
	neg		edx
	neg		eax
	sbb		edx, 0

	ret
ParseRadixSigned	ENDP
Posted on 2001-02-22 15:53:00 by bitRAKE
Hello,

This topic is quite old, sorry for any inconvenience.
I get strange results from the routine in fasm, why?



    format pe console
    entry main
   
  include "include\win32ax.inc"

section ".idata" import data readable writeable

library kernel32, "KERNEL32.DLL", user32, "USER32.DLL"

include "include\api\kernel32.inc"
include "include\api\user32.inc"

.data
big db "FFFFFFFFFFFFFFFF",0
tmp rb 64

.code
main:
mov esi, big
mov edi, 16
call ParseRadixUnsigned

;cinvoke wsprintf, tmp, "EDX=%.8X  EAX=%.8X", edx, eax
;invoke MessageBox, 0, tmp, "", 0
;invoke ExitProcess, 0

;EDX = 0000000F    ; <-- I think this is wrong
;EAX = FFFFFFFF




; Routine for converting an ASCII string to a 64 bit integer in any base.
; X-Calibre, Diamond Crew 2000
ParseRadixUnsigned:
; Input: Pointer to zero-terminated string in ESI, radix in EDI
; Output: Parsed number in EDX::EAX
; Uses: EAX, EBX, ECX, EDX, ESI, EDI

xor ebx, ebx

; result in EDX::EAX
xor eax, eax
xor edx, edx

mov al,
inc esi
test eax, eax
jz @@endOfParsing

sub eax, 30h
.if eax > 9
sub eax, 7
.endif

mov bl,

@@smallParseLoop:
; ASCII to number conversion
sub ebx, 30h
inc esi
mul edi
.if ebx > 9
sub ebx, 7
.endif
add eax, ebx
mov bl,
jc @@carry
test ebx, ebx
jnz @@smallParseLoop

ret

@@carry:
inc edx
test ebx, ebx
jz @@endOfParsing

@@bigParseLoop:
; ASCII to number conversion
mov ecx, eax
mov eax, edx
sub ebx, 30h
inc esi
mul edi
xchg eax, ecx
mul edi
.if ebx > 9
sub ebx, 7
.endif
add eax, ebx
mov bl,
adc edx, ecx

test ebx, ebx
jnz @@bigParseLoop

@@endOfParsing:
ret
Posted on 2010-07-28 16:15:09 by dan88
The problem is that the function expects a certain kind of input parameter, and the caller in "main" is providing another...
Posted on 2010-07-28 18:00:34 by Homer
Homer, if you mean the pointer to the null-terminated string then it is correctly specified (in fasm, "mov esi, big" is equal to MASM's "mov esi, offset big").
Posted on 2010-07-28 20:38:40 by LocoDelAssembly
Hello,

Thanks for the feedback.

I run code in MASM results are also wrong, funny how no one's noticed it, i can't figure out why so far. But notice that when @@smallParseLoop and @@carry subroutines removed, i get 18446744073709551615 which is correct.
Posted on 2010-07-29 09:47:09 by dan88
There's no testing for carry after first mul

ParseRadixUnsigned2:
; Input: Pointer to zero-terminated string in ESI, radix in EDI
; Output: Parsed number in EDX::EAX
; Uses: EAX, EBX, ECX, EDX, ESI, EDI

; result in EDX::EAX
movzx ebx, byte ptr
xor eax, eax
xor edx, edx
xor ecx, ecx
test ebx, ebx
jz @overflow
@@small:
mul edi
inc esi
jc @@car
and ebx, not ' '
sub ebx, '0'-' '+10
.if !sign?
sub ebx,7+' '
.endif
lea eax,
movzx ebx, byte ptr
test ebx,ebx
jnz @@small
ret
@@big:
mov ecx, eax
mov eax, edx
mul edi
xchg eax, ecx
inc esi
mul edi
@@car:
and ebx, not ' '
sub ebx, '0'-' '+10
.if !sign?
sub ebx,7+' '
.endif
add ebx, 10
add eax, ebx
adc edx, ecx
movzx ebx, byte ptr
jc @overflow
test ebx,ebx
jnz @@big
@overflow:
ret
Posted on 2010-07-29 10:47:36 by drizz
Thank you drizz, i see what you mean.
Posted on 2010-07-29 14:47:48 by dan88