fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int itoa(int num, char* str, int len, int base);
  5.  
  6. int main(void) {
  7. char bits[9];
  8. int len;
  9. fprintf(stdout, "bits uc sc\n");
  10. for (int i = 0; i < 0x100; ++i) {
  11. itoa(i, bits, 9, 2);
  12. len = 8 - strlen(bits);
  13. if (len) {
  14. fprintf(stdout, "%0*d%s %4u %4d\n", len, 0, bits, (unsigned char)i, (char)i);
  15. } else {
  16. fprintf(stdout, "%s %4u %4d\n", bits, (unsigned char)i, (char)i);
  17. }
  18. }
  19. return 0;
  20. }
  21.  
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. void
  26. strrev(unsigned char *str);
  27. int
  28. itoa(int num, char* str, int len, int base)
  29. {
  30. int sum = num;
  31. int i = 0;
  32. int digit;
  33. if (len == 0)
  34. return -1;
  35. do
  36. {
  37. digit = sum % base;
  38. if (digit < 0xA)
  39. str[i++] = '0' + digit;
  40. else
  41. str[i++] = 'A' + digit - 0xA;
  42. sum /= base;
  43. }while (sum && (i < (len - 1)));
  44. if (i == (len - 1) && sum)
  45. return -1;
  46. str[i] = '\0';
  47. strrev(str);
  48. return 0;
  49. }
  50.  
  51. void
  52. strrev(unsigned char *str)
  53. {
  54. int i;
  55. int j;
  56. unsigned char a;
  57. unsigned len = strlen((const char *)str);
  58. for (i = 0, j = len - 1; i < j; i++, j--)
  59. {
  60. a = str[i];
  61. str[i] = str[j];
  62. str[j] = a;
  63. }
  64. }
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
bits     uc  sc
00000000    0    0
00000001    1    1
00000010    2    2
00000011    3    3
00000100    4    4
00000101    5    5
00000110    6    6
00000111    7    7
00001000    8    8
00001001    9    9
00001010   10   10
00001011   11   11
00001100   12   12
00001101   13   13
00001110   14   14
00001111   15   15
00010000   16   16
00010001   17   17
00010010   18   18
00010011   19   19
00010100   20   20
00010101   21   21
00010110   22   22
00010111   23   23
00011000   24   24
00011001   25   25
00011010   26   26
00011011   27   27
00011100   28   28
00011101   29   29
00011110   30   30
00011111   31   31
00100000   32   32
00100001   33   33
00100010   34   34
00100011   35   35
00100100   36   36
00100101   37   37
00100110   38   38
00100111   39   39
00101000   40   40
00101001   41   41
00101010   42   42
00101011   43   43
00101100   44   44
00101101   45   45
00101110   46   46
00101111   47   47
00110000   48   48
00110001   49   49
00110010   50   50
00110011   51   51
00110100   52   52
00110101   53   53
00110110   54   54
00110111   55   55
00111000   56   56
00111001   57   57
00111010   58   58
00111011   59   59
00111100   60   60
00111101   61   61
00111110   62   62
00111111   63   63
01000000   64   64
01000001   65   65
01000010   66   66
01000011   67   67
01000100   68   68
01000101   69   69
01000110   70   70
01000111   71   71
01001000   72   72
01001001   73   73
01001010   74   74
01001011   75   75
01001100   76   76
01001101   77   77
01001110   78   78
01001111   79   79
01010000   80   80
01010001   81   81
01010010   82   82
01010011   83   83
01010100   84   84
01010101   85   85
01010110   86   86
01010111   87   87
01011000   88   88
01011001   89   89
01011010   90   90
01011011   91   91
01011100   92   92
01011101   93   93
01011110   94   94
01011111   95   95
01100000   96   96
01100001   97   97
01100010   98   98
01100011   99   99
01100100  100  100
01100101  101  101
01100110  102  102
01100111  103  103
01101000  104  104
01101001  105  105
01101010  106  106
01101011  107  107
01101100  108  108
01101101  109  109
01101110  110  110
01101111  111  111
01110000  112  112
01110001  113  113
01110010  114  114
01110011  115  115
01110100  116  116
01110101  117  117
01110110  118  118
01110111  119  119
01111000  120  120
01111001  121  121
01111010  122  122
01111011  123  123
01111100  124  124
01111101  125  125
01111110  126  126
01111111  127  127
10000000  128 -128
10000001  129 -127
10000010  130 -126
10000011  131 -125
10000100  132 -124
10000101  133 -123
10000110  134 -122
10000111  135 -121
10001000  136 -120
10001001  137 -119
10001010  138 -118
10001011  139 -117
10001100  140 -116
10001101  141 -115
10001110  142 -114
10001111  143 -113
10010000  144 -112
10010001  145 -111
10010010  146 -110
10010011  147 -109
10010100  148 -108
10010101  149 -107
10010110  150 -106
10010111  151 -105
10011000  152 -104
10011001  153 -103
10011010  154 -102
10011011  155 -101
10011100  156 -100
10011101  157  -99
10011110  158  -98
10011111  159  -97
10100000  160  -96
10100001  161  -95
10100010  162  -94
10100011  163  -93
10100100  164  -92
10100101  165  -91
10100110  166  -90
10100111  167  -89
10101000  168  -88
10101001  169  -87
10101010  170  -86
10101011  171  -85
10101100  172  -84
10101101  173  -83
10101110  174  -82
10101111  175  -81
10110000  176  -80
10110001  177  -79
10110010  178  -78
10110011  179  -77
10110100  180  -76
10110101  181  -75
10110110  182  -74
10110111  183  -73
10111000  184  -72
10111001  185  -71
10111010  186  -70
10111011  187  -69
10111100  188  -68
10111101  189  -67
10111110  190  -66
10111111  191  -65
11000000  192  -64
11000001  193  -63
11000010  194  -62
11000011  195  -61
11000100  196  -60
11000101  197  -59
11000110  198  -58
11000111  199  -57
11001000  200  -56
11001001  201  -55
11001010  202  -54
11001011  203  -53
11001100  204  -52
11001101  205  -51
11001110  206  -50
11001111  207  -49
11010000  208  -48
11010001  209  -47
11010010  210  -46
11010011  211  -45
11010100  212  -44
11010101  213  -43
11010110  214  -42
11010111  215  -41
11011000  216  -40
11011001  217  -39
11011010  218  -38
11011011  219  -37
11011100  220  -36
11011101  221  -35
11011110  222  -34
11011111  223  -33
11100000  224  -32
11100001  225  -31
11100010  226  -30
11100011  227  -29
11100100  228  -28
11100101  229  -27
11100110  230  -26
11100111  231  -25
11101000  232  -24
11101001  233  -23
11101010  234  -22
11101011  235  -21
11101100  236  -20
11101101  237  -19
11101110  238  -18
11101111  239  -17
11110000  240  -16
11110001  241  -15
11110010  242  -14
11110011  243  -13
11110100  244  -12
11110101  245  -11
11110110  246  -10
11110111  247   -9
11111000  248   -8
11111001  249   -7
11111010  250   -6
11111011  251   -5
11111100  252   -4
11111101  253   -3
11111110  254   -2
11111111  255   -1