Advertisement
proes2a

問2

Nov 26th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. char toChar(int n){
  4.   if(n < 10) return '0' + n;
  5.   return 'a' + n - 10;
  6. }
  7.  
  8. void printNumAs(unsigned int num, int bit){
  9.   int tmp[256], i, n = pow(2, bit) - 1;
  10.   for(i = 0; num > 0; i++){
  11.     tmp[i] = num & n;
  12.     num >>= bit;
  13.   }
  14.   while(i-- > 0){
  15.     printf("%c", toChar(tmp[i]));
  16.   }
  17. }
  18.  
  19. int main() {
  20.   int n;
  21.  
  22.   printf("10進数 : ");
  23.   scanf("%d", &n);
  24.  
  25.   printf("\n16進数 : ");
  26.   printNumAs(n, 4);
  27.  
  28.   printf("\n 8進数 : ");
  29.   printNumAs(n, 3);
  30.  
  31.   return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement