Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define ABS(n) n >= 0 ? n : -n
- void reverse(char *s)
- {
- int j = 0;
- while (s[j])
- j++;
- j--;
- for(int i = 0; j > i; j--, i++)
- {
- char tmp = s[i];
- s[i] = s[j];
- s[j] = tmp;
- }
- return;
- }
- void itob(int n, char *s, int base)
- {
- int sign = 0;
- if (n < 0)
- sign++;
- char *start = s;
- do
- {
- int curr_numb = n % base;
- curr_numb = ABS(curr_numb);
- switch (curr_numb)
- {
- case 10:
- *s++ = 'A';
- break;
- case 11:
- *s++ = 'B';
- break;
- case 12:
- *s++ = 'C';
- break;
- case 13:
- *s++ = 'D';
- break;
- case 14:
- *s++ = 'E';
- break;
- case 15:
- *s++ = 'F';
- break;
- default:
- *s = curr_numb + '0';
- s++;
- break;
- }
- n /= base;
- } while (n);
- if (sign)
- *s++ = '-';
- *s = '\0';
- reverse(start);
- }
- int main(void)
- {
- int numb = 1369;
- char string[256];
- for (int base = 2; base <= 16; base++)
- {
- printf("\n\n\nBASE IS %i\n", base);
- itob(numb, string, base);
- printf("%i\n%s\n\n", numb, string);
- numb = 1;
- itob(numb, string, base);
- printf("%i\n%s\n\n", numb, string);
- numb = -1;
- itob(numb, string, base);
- printf("%i\n%s\n\n", numb, string);
- numb = -0;
- itob(numb, string, base);
- printf("%i\n%s\n\n", numb, string);
- numb = INT_MAX;
- itob(numb, string, base);
- printf("%i\n%s\n\n", numb, string);
- numb = INT_MIN;
- itob(numb, string, base);
- printf("%i\n%s\n\n", numb, string);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement