Results 1 to 17 of 17

Thread: atoi is screwy

  1. #1

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919

    atoi is screwy

    i have 27015 in a string, and i do atio, i get 325041



    port = atoi((LPCSTR)servern.Right(servern.GetLength() - servern.Find(':') - 1));
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    27015...isn't that Half-Life?

    What's the code around it?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    yeah im querying a half life server.

    Code:
    char *buf = new char[6];
    	
    	if(servern.Find(':') != -1) {
    		port = atoi((LPCSTR)servern.Right(servern.GetLength() - servern.Find(':') - 1));
    	}
    	else port=27015;
    
    	MessageBox(itoa(port,buffer,6),"port");
    
    	return false;
    	//get the IP
    	servern = (servern.Find(':') != -1)  ? servern.Left(servern.Find(':')) : servern;
    
    	if(sock.Create((int)port, SOCK_DGRAM) == 0) return false;	//create the socket
    	if(sock.Connect(servern,port) == 0) return false;									//connect
    	Sleep(200);
    
    	strcpy(send,"ÿÿÿÿinfo\x00");
    	sock.Send(send,strlen(send));
    
    	sock.Receive(buffer,5);
    	MessageBox("5","5");
    
    	char *ret=new char[];
    	bool done=false;
    	char *tmp=new char[1];
    
    	for(int i=0;i<12;i++){
    		while(!done){	
    			sock.Receive(tmp,1);
    			if(tmp=='\x00'){ done=true; } 
    			else{ serverinfo[i]+=tmp; }
    		}	
    		MessageBox((LPCSTR)serverinfo[i],(LPCSTR)serverinfo[i]);
    		done=false;
    	}
    
    	return true;
    im returning false after i get the port because i wanna make sure i get teh port right, its just for debug. the code isnt complete either

    im using cstring until it all works, then ill use char*

    thanks
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    const char *sn = "host.domain.com:27000";
    const char *iter = sn;
    
    while(iter++) {
        if(*iter == ':') {
            break;
        }
    }
    
    if(*iter) {
        port = atoi(iter);
    }
    ...something like that might work if you wanted to use the C style strings
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    yeah i wanna convert it to c-style.

    so i guess atoi doesnt work on cstring?

    and that code just loops through it until it reaches the : character...so i guess ihave to fill it in?
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Well, if it comes out of the loop with a value of ':' then you can send it to atoi. Actually, that code's wrong -- you need to add 1 to the pointer to get the port number (although you might need to verify a little more, like there actually is a port there).

    If it comes out with NULL then it reached the end without finding ':' and you can put 27015 in.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    yeah thats why i had the find there.
    so itll loop through, and i guess toss out the characters until it reachs the :?
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    No, it doesn't change the pointer to the start, it uses a second pointer to traverse the string, since atoi doesn't care where in the string it starts.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    oh i see now
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  10. #10

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    Code:
    	const char *iter = server_name;
    	while(iter++) {
    		if(*iter == ':') {
    			*iter++;
    			break;
    		}
    	}
    	if(*iter) {
    		port = atoi(iter);
    	} else{
    		port = 27015;
    	}

    atoi still gives me 325041

    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    *iter++;
    That can just be ++iter;.

    Can't think why atoi *still* won't give the right number :S
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    its not, i dunno why

    and sprintf has gone funky on me too

    Code:
    	port=27015;
    	char *test=new char[];
    
    	sprintf(test,"%i",&port);
    i outputted it, and got 1241156

    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Try giving that a size:
    Code:
    port=27015;
    char test[40];
    
    sprintf(test, "%i", port);
    ...you need to pass the number, not the pointer as well.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  14. #14

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    hmm now it worked.

    i thought you had to pass a pointer to it? or if that scanf only?

    now to get atoi working..
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  15. #15
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    That's scanf. This is why I like the iostreams stuff...in many cases they're a lot faster as well (all the conversions are resolved at compile-time rather than run-time).

    Actually, tell a lie, some compilers can interpret printf, etc., as intrinsics and they parse the format string themselves, but they're the minority.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  16. #16

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    ah ok. i find it easier to format using printf though. its part of my laziness
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  17. #17

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    ok now it works

    next to figure out why it locks up..thanks for the help
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width