Function bugs
Function bugs
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!
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
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
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
Ok, thanx!
I change this function... now it looks like this and work fine:
P.S - If you see optimizations or bugs, please say me
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;
}
Re:Function bugs
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.
it can't handel numbers over 256.
but probly this won't be a prob.
Re:Function bugs
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]
[tt]Please enter a number. Don't enter numbers higher than 256 or the computer will crash.
>_[/tt]
Re:Function bugs
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
I will add hight number support, I think I found out a way.
It will not crash
Re:Function bugs
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.
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
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];
}