• Welcome to Valhalla Legends Archive.
 

c++ string manipulation

Started by ArticMoogle, October 08, 2003, 05:02 PM

Previous topic - Next topic

Kp

Quote from: iago on October 09, 2003, 12:44 AMatoi() is obviously the one I was looking for.

In this case, 1) he'll always be using base 10, and 2) the pointer will always be at the beginning of the string anyway, and 3) he's a newb to programming, and int itoa(char *num) is a lot easier to use :)
First you say that atoi was indeed the one you wanted, then you go and call it itoa again?  Pay attention! :)  Also, the pointer that strtoul provides on return (via the second argument; see documentation for details) will reliably point at the first non-digit for the current base.  That enables him to support any 32-bit number, regardless of digits, and not expend additional effort on return locating the type specifier.  Using atoi allows any length number, but then requires that he do extra work on return to locate the non-digit (or make the assumption that the number is always a certain quantity of digits long, which is IMO a bad limitation to go imposing if you don't need to do so).
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

iago

Fine then, he should use that one.  Is that what you wanted me to say? :-P

And I always get itoa and atoi mixed up, never tried to keep them straight, but it's clear which one I mean :P
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Adron


char buf[] = "123d 36c 25b";
int counts[256] = {0};
for(char *p = strtok(buf, " "); p; p = strtok(0, " ") {
 int n;
 unsigned char c;
 if(sscanf(p, "%d%c", &n, &c) == 2)
    counts[c] += n;
}