News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Converting a string to a dword?

Started by Sean1337, January 02, 2008, 09:42:31 AM

Previous topic - Next topic

Sean1337

Hey guys,

I would like to know how I could go about converting a string to a 4 byte dword?

For example:

I want to convert the string "50" which is 35 30 in hex to the dword number 50 so I can use it later on in a calculation.

So could anyone help me out here with converting the string "50" to dword? Or any string input to dword, e.g. an input field that takes string input and I want to conver the string input to dword.

Thanks!
-Sean

Jackal

included in the masm32 lib is a function atodw. this should do it for you.

Sean1337

I tried using that to convert the string "6060" to the # 6060 but instead it converted to the # 606.

Jackal

although i think atodw is much better than this function you can try it.


AsciiBase proc uses  esi InPut:DWORD
   xor eax, eax
   mov esi, InPut
   xor ecx, ecx
   xor edx, edx
   mov al, [esi]
   inc esi
   .while al != 0
     sub al, '0'
     lea ecx, [ecx+ecx*4]
     lea ecx, [eax+ecx*2]
     mov al, [esi]
     inc esi
   .endw
   lea eax, [ecx+edx]

   ret
AsciiBase endp

MichaelW

The MASM32 procedures that accept strings, like the C run-time library and Windows API functions that accept strings, expect to be passed the address of a null-terminated string. So assuming that you were working with ANSI strings, if you defined a string like this:

mystring db "6060",0

MASM would encode the string as:

db 36h, 30h, 36h, 30h, 0

Where 36h is the ASCII character code for "6" and 30h the ASCII character code for "0". And if you passed the address of this string to atodw, then atodw should return the value 6060d (or 17ACh or 0001011110101100b) in EAX.

eschew obfuscation

GregL

Sean1337,

It works just fine.


.686
.MODEL FLAT,STDCALL
OPTION CASEMAP:NONE

INCLUDE windows.inc

INCLUDE kernel32.inc
INCLUDE user32.inc
INCLUDE msvcrt.inc
INCLUDE masm32.inc

INCLUDE c:\masm32\macros\macros.asm

INCLUDELIB kernel32.lib
INCLUDELIB user32.lib
INCLUDELIB msvcrt.lib
INCLUDELIB masm32.lib

.DATA

    szStr BYTE "6060", 0

.CODE

start:

    call main
    inkey chr$(13,10,"Press any key to exit ... ")
    INVOKE ExitProcess, eax

main PROC
    INVOKE atodw, ADDR szStr
    print sdword$(eax)
    ret
main ENDP

END start


Sean1337

Well I ended up using the function atoi in ntdll.dll, it worked perfect.

Now another question,

do you guys know how to use the function "atof"? It's more commonly used in c++ however I know its found in the modules msvcrt.dll and msvcr71.dll.

Nevertheless, I am not sure how to handle floats in asm. Say I typed the string "7.5" and passed that as the parameter for the function atof, how would the float number 7.5 be returned? Normal functions return the result in eax however in this case I am not sure as to how this may be done.

Thanks in advance,
-Sean

GregL

Sean1337,

It's returned in ST(0) of the FPU.


.686
.MODEL FLAT,STDCALL
OPTION CASEMAP:NONE

INCLUDE windows.inc

INCLUDE kernel32.inc
INCLUDE user32.inc
INCLUDE msvcrt.inc
INCLUDE masm32.inc

INCLUDE c:\masm32\macros\macros.asm

INCLUDELIB kernel32.lib
INCLUDELIB user32.lib
INCLUDELIB msvcrt.lib
INCLUDELIB masm32.lib

.DATA

    dbl   REAL8 0.0
    szStr BYTE "7.5", 0

.CODE

start:

    call main
    inkey chr$(13,10,"Press any key to exit ... ")
    INVOKE ExitProcess, eax

main PROC
    INVOKE crt_atof, ADDR szStr
    fstp dbl
    print real8$(dbl)
    ret
main ENDP

END start



Sean1337

Thanks Greg,

Appreciate your help, worked perfectly.

Thanks ;)

All the best,
-Sean

ossama

these functions convert string to integer:
StrToInt
StrToInt64Ex
StrToIntEx