Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //u uart.h ide ovo
- void obrni(char *str, int length);
- unsigned char itoa(int num, char *str, int base);
- //ovo ide u uart.c
- unsigned char itoa(int num, char *str, int base) { // prebacije integer u karaktere(ASCII)
- int i = 0;
- int ost;
- bit negative = 0;
- if(num == 0) {
- str[i++] = '0';
- str[i] = '\0';
- return str;
- }
- // negativni brojevi za decimalni sistem, inace su brojevi unsigned
- if (num < 0 && base == 10) {
- negative = 1;
- num = -num;
- }
- while(num != 0) {
- ost = num % base;
- if (ost > 9) {
- str[i++] = (ost - 10) + 'a';
- }
- else {
- str[i++] = ost + '0';
- }
- num = num/base;
- }
- if (negative) {
- str[i++] = '-';
- }
- str[i] = '\0'; // terminiraj string
- // pozivam f-iju koja vraca ispravan redoslijed
- obrni(str, i);
- }
- // funkcija koja ce obrnuti redoslijed karaktera u stringu
- void obrni(char *str, int length) {
- int i = 0;
- int j = length - 1;
- char c;
- while(i < j) {
- c = str[i];
- str[i] = str[j];
- str[j] = c;
- i++;
- j--;
- }
- }
Add Comment
Please, Sign In to add comment