News:

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

Can s.o plz help me [number conversion]

Started by M4D45M, February 05, 2005, 03:55:10 PM

Previous topic - Next topic

M4D45M

Hi there..
I cant figure out how to convert an ASCII number which I receive via TCP/IP to a buffer into the hexadecimal value which is equal to it's decimal value that the ASCII number represents.

sounds quite difficult but with following example it should be clear:
(I am a ftp-client..)

The PASV-ftp-command replies with:
227 Entering Passive Mode (192,168,0,16,198,170).
So the string is stored something like this: 32 32 37 20 .....................................31 39 38...
                                                             (2   2  7 <space>                                1   9  8)

I now have to connect to the replied  adress:port  to send the file data,
thus I have to parse the ip to an 'in_addr'.. 
Is there an API-func. I could use ??
( inet_addr() of wsock-API takes only dotted format, right ?! )

If there isn't, I have to parse it by myself and I dont know how to convert the numbers !!
cuz I have to calculate the port:
  "227 Entering Passive Mode (192,168,0,16,198,170)."
198d * 256d + 170d = 50858d
(that's how it works)

So I must get from '31 39 38' to the hexadecimal value of 198d (=C6h)
and from '31 37 30' to the hexadecimal value of 170d (=AAh)
(so that I can calculate..)
[ C6h * 100h + AAh = C6AAh ] (=50858d)  <= the port the server listens to !

I think there must also be an API for such conversions cuz this must be used very often,
but I couldn't find anything via google

help is very appreciated.
I'm totally stuck  :(
thanx in advance

Jurgen

#1
Hello,

Here is an example code how you could do it: 

.data
stringin db '313938'
stringout db 4 dup (0)

.code
     xor ecx,ecx
     lea esi,stringin+1
     lea edi,stringout
loop:
     mov al, [esi]
     mov [edi],al
     add esi,2
     add edi,1
     add ecx,1
     cmp ecx,3
     jnz loop

     invoke atodw,offset stringout
     the value you want is in eax here


M4D45M


MichaelW

Using fixed offsets to access the string would work OK for a known string, but for the general case where the length of the byte fields could vary (1-3 digits) you would need to locate the byte fields by searching for the delimiters. You could copy each byte field to another string, but unless you need to preserve the original string it would be more efficient to just replace the delimiter at the end of each byte field with a null and use the byte field in place.

I don't know if this is homework, or how you intend to code it, but the CRT strtok function could be to tokenize the string, and it would take care of placing a null at the end of each token for you.

The conversion of each field to a binary value can be done with the MASM32 atodw procedure just as Jurgen demonstrated, or with the CRT atol function.

And for combining the bytes *256d can be efficiently done as SHL 8.
eschew obfuscation

Jurgen

Now that i read your question again, i think that my code wouldn't work.
If '31 39 38' is a string then it works, but if they are byte values, it will not work.
:lol

Jurgen

Btw MichaelW,

I am also interested in the strtok function.
I see there are no import librarys for the strtok function in the lib folder, does this mean I have to load it dynamically ?

MichaelW

eschew obfuscation