News:

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

String manipulation

Started by FlySky, January 10, 2011, 05:57:50 PM

Previous topic - Next topic

FlySky

Good evening guys,

Got this issue I can't seem to solve it's about the ascii string which is read from an editbox.

Let's say I type this string into my editbox: 0a0b0c0d0e0f.

When using the GetDlgItemText API it grabs the string and stores it's ascii presentation into an buffer.

I need to have that ascii buffer converted back to the string I typed how would I do that.

htodw function in the m32lib would do it, but that only works on DWORDS, what if I am having a bigger array.

Got any ideas:

I also coded myself a function which reads all ascii characters one by one and throws it's results in a buffer like:

cmp al '0' --> cmp ascii
jne >next
mov [edi],00h

but than it adds an extra 0 as it's copying a BYTE value.


dedndave

to make a simple one, you could just use the dword routine twice and concatonate the strings
hexidecimal is easy to work with, in that respect

disintx

Quote from: FlySky on January 10, 2011, 05:57:50 PM
Let's say I type this string into my editbox: 0a0b0c0d0e0f.
When using the GetDlgItemText API it grabs the string and stores it's ascii presentation into an buffer.
I need to have that ascii buffer converted back to the string I typed how would I do that.

htodw function in the m32lib would do it, but that only works on DWORDS, what if I am having a bigger array.

Got any ideas:
I also coded myself a function which reads all ascii characters one by one and throws it's results in a buffer like:
cmp al '0' --> cmp ascii
jne >next
mov [edi],00h
but than it adds an extra 0 as it's copying a BYTE value.

I would recommend doing like dedndave said; it doesn't "work on DWORDs", though, it simply returns the characters in the string passed to it as a DWORD.
Meaning, you would need to loop through and call htodw depending on how long your string is.

dedndave

using the masm32 macros would make it pretty simple
have a look at \masm32\help\hlhelp.chm, under Macro Catagories/String Macros

cat$
uhex$
right$

disintx

include masm32rt.inc
;include x:\masm32\macros\macros.asm

.data
szTest db "D34DB33f",0

.code
start:
invoke htodw, addr szTest
print uhex$(eax)
xor eax,eax
ret
end start


Threw this together really quick..uhex$ works well, also: "Each macro call in the assembler source code provides a 20 byte long buffer in the data section to hold the results of the conversion".

dedndave

i had to re-read the original question to see exactly what he was after - a bit mis-worded, there
a little more detail in what is required would be nice
his example simply shows a 48-bit input value
i am going to assume that it might be various sizes, up to 64 bits

it's not super-fast - but it is reasonably small and it works   :P
;place this PROTOtype near the beginning of the program
htoqw   PROTO   :DWORD

;--------------------------------------------------------

        OPTION  PROLOGUE:None
        OPTION  EPILOGUE:None

htoqw   PROC    lpszInputString:DWORD

;Call With: lpszInputString = address of ASCII hex string

;  Returns: EDX:EAX = 64 bit binary value

;It is the callers responsibility to insure that the string length is 16
;characters or less and that it contains no non-hexadecimal characters.

        push    edi
        mov     edi,[esp+8]

;get the length of the string and split it

        INVOKE  szLen,edi
        mov     edx,8
        sub     eax,edx
        ja      htoqw0

        push    0
        add     edx,eax
        pop     eax
        jnz     htoqw1

        mov     edx,eax
        jmp short htoqw2

;temporarily modify the string and evaluate the high-order part

htoqw0: movzx   ecx,byte ptr [edi+eax]
        push    eax
        push    ecx
        mov     [edi+eax],ch
        INVOKE  htodw,edi
        pop     ecx
        pop     edx
        add     edi,edx
        mov     [edi],cl

;evaluate the low-order part

htoqw1: push    eax
        INVOKE  htodw,edi
        pop     edx

htoqw2: pop     edi
        ret     4

htoqw   ENDP

        OPTION  PROLOGUE:PrologueDef
        OPTION  EPILOGUE:EpilogueDef

;--------------------------------------------------------

FlySky

Thanks for all replies. I will be experimenting with the suggestions it's fun to play with, will get back to you guys ASAP.