numerate_number.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of M2-Planet.
  3. *
  4. * M2-Planet is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * M2-Planet is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include<stdlib.h>
  18. #include<string.h>
  19. // void* calloc(int count, int size);
  20. #define TRUE 1
  21. #define FALSE 0
  22. char* numerate_number(int a)
  23. {
  24. char* result = calloc(16, sizeof(char));
  25. int i = 0;
  26. /* Deal with Zero case */
  27. if(0 == a)
  28. {
  29. result[0] = '0';
  30. return result;
  31. }
  32. /* Deal with negatives */
  33. if(0 > a)
  34. {
  35. result[0] = '-';
  36. i = 1;
  37. a = a * -1;
  38. }
  39. /* Using the largest 10^n number possible in 32bits */
  40. int divisor = 0x3B9ACA00;
  41. /* Skip leading Zeros */
  42. while(0 == (a / divisor)) divisor = divisor / 10;
  43. /* Now simply collect numbers until divisor is gone */
  44. while(0 < divisor)
  45. {
  46. result[i] = ((a / divisor) + 48);
  47. a = a % divisor;
  48. divisor = divisor / 10;
  49. i = i + 1;
  50. }
  51. return result;
  52. }
  53. int char2hex(int c)
  54. {
  55. if (c >= '0' && c <= '9') return (c - 48);
  56. else if (c >= 'a' && c <= 'f') return (c - 87);
  57. else if (c >= 'A' && c <= 'F') return (c - 55);
  58. else return -1;
  59. }
  60. int hex2char(int c)
  61. {
  62. if((c >= 0) && (c <= 9)) return (c + 48);
  63. else if((c >= 10) && (c <= 15)) return (c + 55);
  64. else return -1;
  65. }
  66. int char2dec(int c)
  67. {
  68. if (c >= '0' && c <= '9') return (c - 48);
  69. else return -1;
  70. }
  71. int char2oct(int c)
  72. {
  73. if (c >= '0' && c <= '7') return (c - 48);
  74. else return -1;
  75. }
  76. int dec2char(int c)
  77. {
  78. if((c >= 0) && (c <= 9)) return (c + 48);
  79. else return -1;
  80. }
  81. int numerate_string(char *a)
  82. {
  83. int count = 0;
  84. int index;
  85. int negative;
  86. /* If NULL string */
  87. if(NULL == a)
  88. {
  89. return 0;
  90. }
  91. if(0 == a[0])
  92. {
  93. return 0;
  94. }
  95. /* Deal with hex */
  96. else if (a[0] == '0' && a[1] == 'x')
  97. {
  98. if('-' == a[2])
  99. {
  100. negative = TRUE;
  101. index = 3;
  102. }
  103. else
  104. {
  105. negative = FALSE;
  106. index = 2;
  107. }
  108. while(0 != a[index])
  109. {
  110. if(-1 == char2hex(a[index])) return 0;
  111. count = (16 * count) + char2hex(a[index]);
  112. index = index + 1;
  113. }
  114. }
  115. /* Deal with octal */
  116. else if(a[0] == '0')
  117. {
  118. if('-' == a[1])
  119. {
  120. negative = TRUE;
  121. index = 2;
  122. }
  123. else
  124. {
  125. negative = FALSE;
  126. index = 1;
  127. }
  128. while(0 != a[index])
  129. {
  130. if(-1 == char2oct(a[index])) return 0;
  131. count = (8 * count) + char2oct(a[index]);
  132. index = index + 1;
  133. }
  134. }
  135. /* Deal with decimal */
  136. else
  137. {
  138. if('-' == a[0])
  139. {
  140. negative = TRUE;
  141. index = 1;
  142. }
  143. else
  144. {
  145. negative = FALSE;
  146. index = 0;
  147. }
  148. while(0 != a[index])
  149. {
  150. if(-1 == char2dec(a[index])) return 0;
  151. count = (10 * count) + char2dec(a[index]);
  152. index = index + 1;
  153. }
  154. }
  155. if(negative)
  156. {
  157. count = count * -1;
  158. }
  159. return count;
  160. }