Page 1 of 1
Function bugs
Posted: Sun Oct 27, 2002 12:12 pm
by Unexpected
I wrote this function...
This function must integer number convert to string.
Example:
Input: int a = 123;
Output: char b[3] = "123";
char *str(int number);
char numeral[9] = "123456798";
char *str(int number)
{
int m, count = 0;
char *string;
while(number != 0)
{
count++;
if (number < 9)
{
m = number;
number = 0;
}
else
{
m = number % 10;
number = (number - m) / 10;
}
string = new char[count];
string[count] = numeral[m];
}
return string;
}
This function doesn't work... Can you help me?
Thanx!
Re:Function bugs
Posted: Sun Oct 27, 2002 12:17 pm
by Tom
ok, here is a working itoa for you
oh, i'm in windows....be back later to give it to you...just a min...
you also need my putch from my pk0.6. download it.
you also also need my start_textmode() in pk0.6
Re:Function bugs
Posted: Sun Oct 27, 2002 12:24 pm
by Tom
[attachment deleted by admin]
Re:Function bugs
Posted: Sun Oct 27, 2002 4:54 pm
by Unexpected
Ok, thanx!
I change this function... now it looks like this and work fine:
Code: Select all
unsigned char *str(int num)
{
int k, i = 0;
unsigned char tmpstr[10], string[10], negative = 0;
if (num < 0)
{
negative = 1;
num = - num;
}
do {
k = num % 10;
num = (num - k) / 10;
tmpstr[i] = numeral[k];
i++;
} while (num != 0);
if (negative == 1)
tmpstr[i] = '-';
for (k = 0; k <= i; k++)
string[k] = tmpstr[i - k];
return string;
}
P.S - If you see optimizations or bugs, please say me
Re:Function bugs
Posted: Sun Oct 27, 2002 6:15 pm
by Tom
there is a bug with my convert int to string...
it can't handel numbers over 256.
but probly this won't be a prob.
Re:Function bugs
Posted: Tue Oct 29, 2002 12:44 pm
by Tim
Yep, you could write programs like this:
[tt]Please enter a number. Don't enter numbers higher than 256 or the computer will crash.
>_[/tt]
Re:Function bugs
Posted: Tue Oct 29, 2002 2:14 pm
by Tom
hahaha. not - my convertInttoString just does things like -113
I will add hight number support, I think I found out a way.
It will not crash
Re:Function bugs
Posted: Tue Oct 29, 2002 3:22 pm
by PlayOS
Hi,
Maybe I am wrong but in this function, the string[10] is created on the stack for that function, when it returns the variable string[10] is destroyed, or at least in a completely working OS it would be. The reason that it would appear to work is because the stack is not erased, esp is just restored, so the string still exists in memory, however in a fully working OS memory references like this will not stay around for long.
Is this right? Anyone.
Re:Function bugs
Posted: Wed Oct 30, 2002 3:54 am
by Unexpected
Yeah in my function is still bug...
I will try to correct...
Re:Function bugs
Posted: Wed Oct 30, 2002 9:47 am
by Unexpected
Now is withuoot any bugs
(I hope ;D )
Code: Select all
void str(int num, char *string)
{
int k, i = 0;
char tmpstr[10], negative = 0;
if (num < 0)
{
negative = 1;
num = - num;
}
do {
k = num % 10;
num = (num - k) / 10;
tmpstr[i] = k + 0x30;
i++;
} while (num != 0);
if (negative == 1)
tmpstr[i] = '-';
for (k = 0; k < i; k++)
string[k] = tmpstr[i - k - 1];
}