The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Telefunken on July 22, 2006, 05:45:20 PM

Title: Convert something to a string
Post by: Telefunken on July 22, 2006, 05:45:20 PM
I realize i ask a lot of questions on this forum... and i feel like a total noob  :red hehheh

Is there a way to convert something into a string. For instance, if I wanted to turn EAX into a string so i could save it's value to a file?
Title: Re: Convert something to a string
Post by: Ian_B on July 22, 2006, 07:24:56 PM
Explain "string"...  :bdg  A string can be anything you want it to mean. Any arbitrary series of bytes can be a "string", so any value in EAX is a string that can be written/saved. Do you mean a null-terminated and readable series of ASCII characters?

If you just want to save a value to a file, you don't NEED to convert it to anything, just save the byte sequence. You just need to know what that byte sequence at that offset in the file represents if you retrieve it later. If you want to convert a numerical value to a human-readable ASCII representation in a file, then there are functions to do that - check the MASM library and the Laboratory forum here for discussion about such things - you're probably looking for such things as DWtoA (DWORD to ASCII).

Be aware that writing an ASCII representation to a file means that if you want to re-load it later, you'll have to reparse it from ASCII back into a number. If you don't really need it to be human-readable, then it's much quicker not to bother and just save the values direct. You just have to know exactly what the length of your records are, what format you saved the data in and what order. Which method is safer is debatable. Which is quicker isn't...  :bg

Ian_B
Title: Re: Convert something to a string
Post by: Telefunken on July 22, 2006, 07:39:37 PM
Yes, that is exactly what i meant, thank you.  :U

Maybe I can be more specific with my question.

If i try this...
Quoteinvoke SendDlgItemMessage,hWin,1002,HKM_GETHOTKEY,0,0
            movzx ecx, al
            movzx edx, ah
         invoke WritePrivateProfileString,addr iniKeySec,addr key1box,edx,addr inifile

...my program crashes
How can i fix this?
Title: Re: Convert something to a string
Post by: Mark Jones on July 22, 2006, 08:16:51 PM
It crashes because WritePrivateProfileString expects a memory offset as the argument you supplied using EDX, where it got a value (AH) instead. It takes that value and tries to read bytes starting from its value as a memory offset.

You have to put the contents of AH into memory somewhere first (say MOV BYTE PTR [szMyVar],AH), then pass that memory address (ADDR szMyVar) to WritePrivateProfileString so that it gets the memory offset it's looking for. szMyVar has to be two or more bytes in length, so that there is guaranteed a terminating null.
Title: Re: Convert something to a string
Post by: Telefunken on July 22, 2006, 10:55:38 PM
Thanks Mark  :U

So far, I haven't been able to find any documentation about converting dword to ASCII. But with regard to converting it back, I remember using a function in an old program of mind called StrToInt, which worked great. Does anyone know where i can get some information about DWtoA or Dword to ASCII?  :dazzled:
Title: Re: Convert something to a string
Post by: Ian_B on July 22, 2006, 11:16:22 PM
There's a handy search feature on this forum... :wink  but this thread I started on an optimised general QWORD to ASCII function has only just reached the second page of The Laboratory: http://www.masm32.com/board/index.php?topic=3051.msg24028#msg24028

You will find other algorithmic approaches elsewhere, many smaller if all you want to do is convert DWORDs. Or you can cut this one down. Try to find P Dixon's original DWORD to ASCII code (search the old forum site) if you are targetting AMD processors rather than Intel, as it uses a MUL-based algorithm which is much faster on non-Intel processors.

Ian_B
Title: Re: Convert something to a string
Post by: zooba on July 23, 2006, 12:39:13 AM
There are functions in MASM32.LIB (check the help file) as well as many other libraries (including my own (http://www.masm32.com/board/index.php?topic=3148.45)) to do this.

Cheers,

Zooba :U
Title: Re: Convert something to a string
Post by: Ian_B on July 23, 2006, 10:58:19 AM
Quote from: zooba on July 23, 2006, 12:39:13 AM... as well as many other libraries to do this.

You can teach a hungry man to fish...   :thumbu

... or you can just give him a neatly prepackaged black-box Swiss knife from which he'll learn nothing about good coding techniques, different coding styles, highly different approaches to apparently simple algorithms etc. which will improve his overall understanding.  :tdown

Let's hope his helpful magic fish-making machine never breaks down.  :eek

Ian_B
Title: Re: Convert something to a string
Post by: Mark Jones on July 23, 2006, 05:05:31 PM
In regards to integer-to-ASCII conversion, consider that an integer byte "0" is the same as ASCII byte "30h". Therefore, 0:9 = 30h:39h. Tinker around with that idea for awhile and you're guaranteed to learn some cool things. :thumbu
Title: Re: Convert something to a string
Post by: Telefunken on July 23, 2006, 05:26:32 PM
Quote from: Ian_B on July 23, 2006, 10:58:19 AM
Quote from: zooba on July 23, 2006, 12:39:13 AM... as well as many other libraries to do this.

You can teach a hungry man to fish...   :thumbu

... or you can just give him a neatly prepackaged black-box Swiss knife from which he'll learn nothing about good coding techniques, different coding styles, highly different approaches to apparently simple algorithms etc. which will improve his overall understanding.  :tdown

Let's hope his helpful magic fish-making machine never breaks down.  :eek

Ian_B


Whaaaaa??  :P

Thanks for the idea Mark Jones, and zooba for the awesome library. I have to figure out how to work it though :bg, but no worries, I'll get it.  :U
Title: Re: Convert something to a string
Post by: zooba on July 24, 2006, 05:30:11 AM
Quote from: Ian_B on July 23, 2006, 10:58:19 AM
Quote from: zooba on July 23, 2006, 12:39:13 AM... as well as many other libraries to do this.

You can teach a hungry man to fish...   :thumbu


No harm in showing him what a fish looks like first :wink And nothing wrong with wanting to eat fish before you know how to catch one

Cheers,

Zooba :U
Title: Re: Convert something to a string
Post by: hutch-- on July 24, 2006, 06:26:03 AM
Yes,

Library code is very useful in that it helps people to get their code up and going quickly but Ian has a good point that I agree with, over time it is a good idea to learn how to write the code that the library contains.
Title: Re: Convert something to a string
Post by: zooba on July 24, 2006, 08:37:06 AM
Quote from: hutch-- on July 24, 2006, 06:26:03 AM
Library code is very useful in that it helps people to get their code up and going quickly but Ian has a good point that I agree with, over time it is a good idea to learn how to write the code that the library contains.

Never said I disagree (hence, writing my own library), but converting a number to a string wasn't the first thing I wanted to do in assembly language (I can't remember what the first thing I wanted to do was... but I'm sure it wasn't that :wink ). If you want to tinker with the engine you shouldn't need to make the wheels first (okay, I'll stop with the analogies)

Cheers,

Zooba :U