3.5.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //itob(n,s,b) that converts the integer n into a base b character representation in the string s
  2. //In particular, itob(n,s,16) formats s as a hexadecimal integer in s.
  3. #include <stdio.h>
  4. void itob(int n, char s[], int b);
  5. int clear(char* c, int count);
  6. void reverse(char s[]);
  7. #define MAGIC_1 2048
  8. #define MIN_INT -2147483648
  9. int main()
  10. {
  11. char s[MAGIC_1];
  12. int k= MIN_INT;
  13. // int l = -k;
  14. //printf("l: %i\n",l);
  15. // itoa( -2147483648,s);
  16. // printf("%s\n",s);
  17. for(;;)
  18. {
  19. clear(s,MAGIC_1);
  20. itob(k,s,2);
  21. printf("b: %i, %i :\t %s\n",2,k,s);
  22. clear(s,MAGIC_1);
  23. itob(k,s,8);
  24. printf("b: %i, %i :\t %s\n",8,k,s);
  25. clear(s,MAGIC_1);
  26. itob(k,s,10);
  27. printf("b: %i, %i :\t %s\n",10,k,s);
  28. clear(s,MAGIC_1);
  29. itob(k,s,16);
  30. printf("b: %i, %i :\t %s\n",16,k,s);
  31. clear(s,MAGIC_1);
  32. itob(k,s,64);
  33. printf("b: %i, %i :\t %s\n",64,k,s);
  34. k*=2;
  35. }
  36. return 0;
  37. }
  38. void itob(int n, char s[], int b)
  39. {
  40. int i, sign, specialcase=0;
  41. if ((sign = n) < 0) /* record sign */
  42. {
  43. if (n != MIN_INT)
  44. n = -n; /* make n positive */
  45. else //special case
  46. {
  47. n++;
  48. n = -n;
  49. specialcase=1;
  50. }
  51. }
  52. i = 0;
  53. do { /* generate digits in reverse order */
  54. if ((i == 0) && specialcase )
  55. {
  56. s[i] = n % b + '1';
  57. if (s[i] > '9') s[i]+=7;
  58. i++;
  59. }/* get next digit */
  60. else
  61. {
  62. s[i] = n % b + '0'; /* get next digit */
  63. if (s[i] > '9') s[i]+=7;
  64. i++;
  65. }
  66. }
  67. while ((n /= b) > 0); /* delete it */
  68. // if (specialcase && b==2) //binary is kind of special.
  69. // {
  70. // s[i]='1';
  71. // i++;
  72. // }
  73. if (specialcase)
  74. {
  75. for(int k =0 ;k<=i+1;k++)
  76. if (((b <= 9) && (s[k] >= b+'0')) || ((b > 9) && (s[k] >= b+'0'+7)))
  77. {
  78. s[k]='0';
  79. if (((s[k+1]==0) && (b==2)) || (s[k+1]=='0')) s[k+1]='1';
  80. else s[k+1]++;
  81. }
  82. }
  83. if (specialcase && b==2) i++; //this is the only one that necessitates one more position i think?
  84. if (sign < 0)
  85. s[i++] = '-';
  86. s[i] = '\0';
  87. reverse(s);
  88. }
  89. void reverse(char s[])
  90. {
  91. if (s!=0)
  92. {
  93. int i=0;
  94. while (s[i]!=0)
  95. i++;
  96. char temp[i];
  97. for (int j=0; j<i; j++)
  98. {
  99. temp[j]=s[i-(j+1)];
  100. }
  101. for (int j=0; j<i; j++)
  102. {
  103. s[j]=temp[j];
  104. s[j+1]=0;
  105. }
  106. }
  107. }
  108. int clear(char* c, int count)
  109. {
  110. for (int i=0; i<count;i++)
  111. {c[i]=0;}
  112. return 0;
  113. }