The MASM Forum

General => The Campus => Topic started by: gelatine1 on January 14, 2016, 09:40:55 PM

Title: string to integer
Post by: gelatine1 on January 14, 2016, 09:40:55 PM
Is there a macro somewhere to convert a string to an integer?
Title: Re: string to integer
Post by: jj2007 on January 14, 2016, 11:09:36 PM
Val("123") (http://www.webalice.it/jj2006/MasmBasicQuickReference.htm#Mb1202) does it, but there must be something in \masm32\macros\macros.asm, too.

For bigger integers, I recommend MovVal:

include \masm32\MasmBasic\MasmBasic.inc      ; download (http://masm32.com/board/index.php?topic=94.0)
  Init
  MovVal xmm0, "1234567890123456789"
  Inkey Str$("xmm0=%i", xmm0)
EndOfCode

;)
Title: Re: string to integer
Post by: gelatine1 on January 15, 2016, 12:11:07 AM
.data?
MyTempString db 4 dup(?)
startpos dd ?
endpos dd ?

.code
invoke GetDlgItemText,hWnd,IDC_EDIT_START,ADDR MyTempString,4
mov startpos,val(MyTempString)
invoke GetDlgItemText,hWnd,IDC_EDIT_END,ADDR MyTempString,4
mov endpos,val(MyTempString)


This code gives me the "error A2039: line too long" error on the lines containing val. They're shorter than the GetDlgItemText lines so I don't understand why it is saying that ? :S
Title: Re: string to integer
Post by: TWell on January 15, 2016, 12:17:33 AM
BTW: GetDlgItemInt() gives integer too ;)
Title: Re: string to integer
Post by: gelatine1 on January 15, 2016, 12:41:24 AM
I've been searching through the masm macro's and Ive found that uval and sval should do the trick.

Quote from: TWell on January 15, 2016, 12:17:33 AM
BTW: GetDlgItemInt() gives integer too ;)

Wow hahaha, I didn't know of it's existence. makes my life easier, thanks! :)
Title: Re: string to integer
Post by: dedndave on January 15, 2016, 01:23:03 AM
crt_atoi (unsigned)
crt_atol (signed)
Title: Re: string to integer
Post by: jj2007 on January 15, 2016, 01:52:11 AM
Quote from: gelatine1 on January 15, 2016, 12:11:07 AMThis code gives me the "error A2039: line too long" error on the lines containing val.

Interesting :t

include \masm32\include\masm32rt.inc
.code
tx123 db "123", 0
start:
  mov eax, val(offset tx123)
  print str$(eax), 13, 10
  print str$(val("123")), 13, 10
  print str$(val(offset tx123))
  exit
end start


Bug confirmed, but try again with JWasm, AsmC, Masm 8 or higher 8)
Title: Re: string to integer
Post by: ragdog on January 15, 2016, 04:42:51 AM
Hello

Shlwapi

Quote
Converts a string that represents a decimal value to an integer. The StrToLong macro is identical to this function.
Syntax
C++
int StrToInt(
  _In_ PCTSTR pszSrc
);


or

Quote
StrToIntEx function
Converts a string representing a decimal or hexadecimal number to an integer.

https://msdn.microsoft.com/en-us/library/windows/desktop/bb773451%28v=vs.85%29.aspx
Title: Re: string to integer
Post by: jj2007 on January 15, 2016, 05:26:24 AM
For those who find Val() (http://www.webalice.it/jj2006/MasmBasicQuickReference.htm#Mb1202) too complicated:

include \masm32\include\masm32rt.inc

txt2dw MACRO sz$
  invoke atodw, reparg(sz$) ; good ol' Masm32
  EXITM <eax>
ENDM
 
.code
tx123 db "123", 0

start:
  print str$(txt2dw(addr tx123)), 9, "tx123", 13, 10
  print str$(txt2dw("456")), 9, "456", 13, 10
  print str$(txt2dw(chr$("789"))), 9, "chr$(789)", 13, 10

  inkey "that was easy, right?"
  exit
end start