Jak w temacie. Potrzebuję zrobić dzielenie modulo w SDCC na At89S8252. Jak mam to zrobić ? :>
Czy wolisz polską wersję strony elektroda?
Nie, dziękuję Przekieruj mnie tam
void ultoa(unsigned long liczba,char* lancuch,char dlugosc)
{
unsigned char cyfra;
unsigned long bufor;
while (dlugosc > 0)
{
cyfra = 0;
cyfra = liczba % 10;
cyfra = cyfra | 0x30;
lancuch[dlugosc-1] = cyfra;
liczba = (float) liczba / 10;
dlugosc--;
}
}
void ultoa(unsigned long liczba,char* lancuch,char dlugosc)
{
unsigned char cyfra;
lancuch[dlugosc]=0; // dodalem
while ((dlugosc > 0) & (liczba!=0)) //zmienilem
{
cyfra = 0;
cyfra = liczba % 10;
cyfra = cyfra | 0x30;
dlugosc--; // zamienilem
lancuch[dlugosc] = cyfra; //
liczba = liczba / 10; // wywalilem (float)
}
while (dlugosc--) //dodalem
lancuch[dlugosc]=32; // 32==spacja
}
#include <at89S8252.h>
//...
#define MAXDIGITS 10 //decymalny
char* ul2decstr(unsigned long x,unsigned char* y)
{
y+=MAXDIGITS;
*y=0x00;
while(x)
{
y--;
*y=(x % 10) | 0x30;
x/=10;
}
return y;
}
void main()
{
unsigned long xxx = 0xFFFFFFFF;
char buf[MAXDIGITS+1], *ptr;
ptr=ul2decstr(xxx, buf);
//...
while(1);
}
Zaquadnik napisał:Zumek, przyznam, że nie bardzo rozumiem Twój kod, możesz go troszkę skomentować ? Będę wdzięczny.
char * ul2decstr(unsigned long x,unsigned char * y)
{
y+=MAXDIGITS;//przesunięcie wskaźnika o max możliwych cyfr
*y=0x00; //znacznik końca stringu - równoważne z y[0]=0x00;
//zwane inaczej operatorem wyłuskania
do{
y--;
*y=(x % 10) | 0x30; //równoważne z y[0]=(x % 10) | 0x30;
x/=10; //równoważne z x = x / 10;
} while(x);
return y;
}
void reverse(char *s)
{
char c, *p, *q;
q = s;
while (*(++q));
for (p=s; p < --q; p++) {
c = *p;
*p = *q;
*q = c;
}
}
void ultoa(unsigned long n, char* buf)
{
char *modbuf = buf;
while (n != 0) {
unsigned char remainder = (unsigned char)(n % 10);
*modbuf = remainder + '0';
n /= 10;
modbuf++;
}
*modbuf = '\0';
reverse(buf);
}
int main() {
unsigned long int a = 132423234UL;
char buf[30];
ultoa(a, buf);
printf("%s\n", buf);
}