News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

string to integer

Started by gelatine1, January 14, 2016, 09:40:55 PM

Previous topic - Next topic

gelatine1

Is there a macro somewhere to convert a string to an integer?

jj2007

Val("123") 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
  Init
  MovVal xmm0, "1234567890123456789"
  Inkey Str$("xmm0=%i", xmm0)
EndOfCode

;)

gelatine1

.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

TWell

BTW: GetDlgItemInt() gives integer too ;)

gelatine1

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! :)

dedndave

crt_atoi (unsigned)
crt_atol (signed)

jj2007

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)

ragdog

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

jj2007

For those who find Val() 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