void iToStr(int x, char res[20], int d)
{
if(x == 0) {
strcpy(res,"0");
if(d==2) strcpy(res,"00");
if(d==3) strcpy(res,"000");
return;
}
bool neg = false;
if(x < 0) {
x = x * -1;
neg = true;
}
int i = 0;
while (x) {
res[i++] = (x % 10) + '0';
x = x / 10;
}
while (i < d)
res[i++] = '0';
reverse(res, i);
res[i] = '\0';
if(neg) {
char temp1[20];
strcpy(temp1,"-");
strcat(temp1,res);
strcpy(res,temp1);
}
}
void bToStr(byte x, char res[20], int d)
{
if(x == 0) {
strcpy(res,"0");
return;
}
int i = 0;
while (x) {
res[i++] = (x % 10) + '0';
x = x / 10;
}
while (i < d)
res[i++] = '0';
reverse(res, i);
res[i] = '\0';
}
void ulongToStr(unsigned long x, char res[20], int d)
{
if(x == 0) {
strcpy(res,"0");
if(d==2) strcpy(res,"00");
if(d==3) strcpy(res,"000");
return;
}
int i = 0;
while (x) {
res[i++] = (x % 10) + '0';
x = x / 10;
}
while (i < d)
res[i++] = '0';
reverse(res, i);
res[i] = '\0';
}
void fToStr(float n, char res[20], int afterpoint)
{
bool neg = false;
if(n < 0) {
n = n * -1;
neg = true;
}
int ipart = (int)n;
int i = 0;
float fpart = n - (float)ipart;
if(ipart == 0) {
strcpy(res,"0");
i = 1;
} else {
i = intToStr(ipart, res, 0);
}
if (afterpoint != 0) {
res[i] = '.';
fpart = fpart * pow(10, afterpoint);
intToStr((int)fpart, res + i + 1, afterpoint);
}
if(neg) {
char temp1[20];
strcpy(temp1,"-");
strcat(temp1,res);
strcpy(res,temp1);
}
}
void reverse(char* str, int len)
{
int i = 0, j = len - 1, temp;
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
}
int intToStr(int x, char str[], int d) //only used for the float function
{
int i = 0;
while (x) {
str[i++] = (x % 10) + '0';
x = x / 10;
}
while (i < d)
str[i++] = '0';
reverse(str, i);
str[i] = '\0';
return i;
}