|
-
Aug 7th, 2002, 01:34 PM
#1
Thread Starter
Fanatic Member
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?
-
Aug 7th, 2002, 01:39 PM
#2
Monday Morning Lunatic
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
-
Aug 7th, 2002, 01:42 PM
#3
Thread Starter
Fanatic Member
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?
-
Aug 7th, 2002, 01:46 PM
#4
Monday Morning Lunatic
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
-
Aug 7th, 2002, 01:55 PM
#5
Thread Starter
Fanatic Member
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?
-
Aug 7th, 2002, 02:01 PM
#6
Monday Morning Lunatic
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
-
Aug 7th, 2002, 02:12 PM
#7
Thread Starter
Fanatic Member
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?
-
Aug 7th, 2002, 02:14 PM
#8
Monday Morning Lunatic
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
-
Aug 7th, 2002, 02:16 PM
#9
Thread Starter
Fanatic Member
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Aug 7th, 2002, 02:19 PM
#10
Thread Starter
Fanatic Member
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?
-
Aug 7th, 2002, 02:21 PM
#11
Monday Morning Lunatic
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
-
Aug 7th, 2002, 03:47 PM
#12
Thread Starter
Fanatic Member
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Aug 7th, 2002, 03:50 PM
#13
Monday Morning Lunatic
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
-
Aug 7th, 2002, 03:57 PM
#14
Thread Starter
Fanatic Member
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?
-
Aug 7th, 2002, 03:59 PM
#15
Monday Morning Lunatic
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
-
Aug 7th, 2002, 04:05 PM
#16
Thread Starter
Fanatic Member
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?
-
Aug 7th, 2002, 04:07 PM
#17
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|