Guest User

inttobin

a guest
Apr 21st, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. char *itoa(unsigned int val, int base)
  5. {
  6.     static char buf[32] = {0};
  7.     int i = 32;
  8.  
  9.     for(; val && i; --i, val /= base)
  10.         buf[i] = "0123456789abcdef"[val % base];
  11.        
  12.     return &buf[i+1];
  13. }      
  14.  
  15. int main (void)
  16. {
  17.     int n;
  18.    
  19.     printf("Enter  a value: ");
  20.     scanf("%d", &n);
  21.     printf("%d -> hex %s\n", n, itoa(n, 16));
  22.     printf("%d -> oct %s\n", n, itoa(n, 8));
  23.     printf("%d -> bin %s\n", n, itoa(n, 2));
  24.     system("pause > nul");
  25.     return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment