Page 1 of 1
C++ Problems
Posted: Mon Oct 07, 2002 11:00 pm
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?
RE:C++ Problems
Posted: Tue Oct 08, 2002 11:00 pm
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
RE:C++ Problems
Posted: Thu Oct 10, 2002 11:00 pm
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
RE:C++ Problems
Posted: Fri Oct 11, 2002 11:00 pm
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...
RE:C++ Problems
Posted: Tue Oct 15, 2002 11:00 pm
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.
RE:C++ Problems
Posted: Tue Oct 15, 2002 11:00 pm
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);
}
RE:C++ Problems
Posted: Wed Oct 16, 2002 11:00 pm
by VoidLogic
No you guys don't understand my problem, look to my latest post as of 10/17/2002