Цитата
!!! вопрос как отобразить на LCD числовые значения заданной переменной
например
int X;
X=4123;
Код
/*
=====================================================================
itoa.
=====================================================================
*/
char* itoa (int value, char *string, int radix)
{
//~~~~~~~~~~~~~~~~~~~~~~~~~
char *dst;
char digits[32];
unsigned int x;
int i, n;
//~~~~~~~~~~~~~~~~~~~~~~~~~
dst = string;
if (radix < 2 || radix > 36)
{
*dst = 0;
return string;
}
if (radix == 10 && value < 0)
{
*dst++ = '-';
x = -value;
}
else x = value;
i = 0;
do
{
n = x % radix;
digits[i++] = (n < 10 ? (char)n+'0' : (char)n-10+'a');
x /= radix;
} while (x != 0);
while (i > 0) *dst++ = digits[--i];
*dst = 0;
return string;
}
Попроще
Код
/*
=====================================================================
"Переворачивает" строку символов.
=====================================================================
*/
void strreverse(char* begin, char* end)
{
char aux;
while(end > begin) aux=*end, *end--=*begin, *begin++=aux;
}
/*
=====================================================================
Перевод числа в строку символов.
=====================================================================
*/
char* itoa(int value, char* result, unsigned char base)
{
//~~~~~~~~~~~~~~~~~~~
char* out = result;
int quotient;
//~~~~~~~~~~~~~~~~~~~
// check that the base if valid
if (base < 2 || base > 16)
{
*result = 0;
return result;
};
if (value >= 0) quotient = value;
else quotient = -value;
do
{
*out = "0123456789ABCDEF"[quotient % base];
++out;
quotient /= base;
} while (quotient);
// Only apply negative sign for base 10
if (value < 0 && base == 10) *out++ = '-';
strreverse(result, out-1);
*out = 0;
return result;
}
Еще проще
Код
/*
=====================================================================
Перевод числа в строку ASCII символов.
Принимает максимальное значение для перевода 65535.
Результат -> в массиве char`ов.
=====================================================================
*/
void IntToStr (unsigned int val, char str[])
{
//~~~~~~~~~~~~~~~~~~~~~~~
signed char i = 0, j = 0;
unsigned char digits[5];
//~~~~~~~~~~~~~~~~~~~~~~
while (val)
{
digits[i] = val % 10; i++; val = val / 10;
};
i--;
while ( i >= 0 )
{
str[j] = (digits[i]+ 0x30); j++; i--;
};
str[j] = 0x00;
}
И здесь посмотрите
http://www.jb.man.ac.uk/~slowe/cpp/itoa.html