C++ Problems

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
VoidLogic

C++ Problems

Post by VoidLogic »

  int* Bob = atoi(BobPtr.c_str());
                    ^ this str has a value i want to covert to a int*
  Any ideas on how to do this, in C, or ASM ?

  P.S. How do you guess do you EPCs?
  
anton

RE:C++ Problems

Post by anton »

I think you ment
int Bob = atoi(BobPtr.c_str());

well, you need to write a parser for it: it is VERY simple, like this:

int atoi(char *buf){
int n=0;
int i;
for(i=0;;i++){
  int a;
  if( (a=char2int(buf[i])) == -1)break;
  n = n*10+a;
}
return n;
}

int char2int(char a){
switch(a){
case '0':return 0;
....
case '9':return 9;
default: return -1;
}
}

Anton
Graanco

RE:C++ Problems

Post by Graanco »

Doesn't C have the ability to convert char to there ASCII equalent???? Then if looking at it to make the number '1' just minus the places amount of the value '0' hold in ASCII???? Just a guess though....

Benjamin
Graanco

RE:C++ Problems

Post by Graanco »

int atoi(char * z){
int x,i,n=0,h=0;
x=sizeof(z);
for(i=x-2;i>=0;i--){
h+=((int)z[i]-(int)'0') * (int)pow(10,n);
n++;
}
return h;
}
Good example of what I am talking about... Part of my old/new OS titled Nodis...
anton

RE:C++ Problems

Post by anton »

First, It will parse "123dasdad" incorectly.
Second, (int)z[i]-(int)'0' is a programming trick, i will confuse the reader who asked the question.
Third, It is not a good programming style to asume something about lowlevel architecure. For example, if it used unnicode chars, then you will have a problem.

Anton.
Keith

RE:C++ Problems

Post by Keith »

Quick and dirty atoi()-like example

int     matoi(const char *p)
{
        int     i, result = 0, x = 0;

        if(p[i = 0] == '-') { x++; i++; }
        while(p[i])
        {
                if((p[i] - '0') > 9)
                {
                        i++;
                        continue;
                }
                result = (result * 10) + (p[i++] - '0');
        }

        return (x ? -(result) : result);
}
VoidLogic

RE:C++ Problems

Post by VoidLogic »

No you guys don't understand my problem, look to my latest post as of 10/17/2002
Post Reply