conv2str.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //conv2str.cpp - Float conversion utilities
  2. #include "conv2str.h"
  3. #include <stdlib.h>
  4. // convert float to string with +123.4 format
  5. char conv[8];
  6. char *ftostr3(const float &x)
  7. {
  8. return itostr3((int)x);
  9. }
  10. char *itostr2(const uint8_t &x)
  11. {
  12. //sprintf(conv,"%5.1f",x);
  13. int xx = x;
  14. conv[0] = (xx / 10) % 10 + '0';
  15. conv[1] = (xx) % 10 + '0';
  16. conv[2] = 0;
  17. return conv;
  18. }
  19. // Convert float to string with 123.4 format, dropping sign
  20. char *ftostr31(const float &x)
  21. {
  22. int xx = x * 10;
  23. conv[0] = (xx >= 0) ? '+' : '-';
  24. xx = abs(xx);
  25. conv[1] = (xx / 1000) % 10 + '0';
  26. conv[2] = (xx / 100) % 10 + '0';
  27. conv[3] = (xx / 10) % 10 + '0';
  28. conv[4] = '.';
  29. conv[5] = (xx) % 10 + '0';
  30. conv[6] = 0;
  31. return conv;
  32. }
  33. // Convert float to string with 123.4 format
  34. char *ftostr31ns(const float &x)
  35. {
  36. int xx = x * 10;
  37. //conv[0]=(xx>=0)?'+':'-';
  38. xx = abs(xx);
  39. conv[0] = (xx / 1000) % 10 + '0';
  40. conv[1] = (xx / 100) % 10 + '0';
  41. conv[2] = (xx / 10) % 10 + '0';
  42. conv[3] = '.';
  43. conv[4] = (xx) % 10 + '0';
  44. conv[5] = 0;
  45. return conv;
  46. }
  47. char *ftostr32(const float &x)
  48. {
  49. long xx = x * 100;
  50. if (xx >= 0)
  51. conv[0] = (xx / 10000) % 10 + '0';
  52. else
  53. conv[0] = '-';
  54. xx = abs(xx);
  55. conv[1] = (xx / 1000) % 10 + '0';
  56. conv[2] = (xx / 100) % 10 + '0';
  57. conv[3] = '.';
  58. conv[4] = (xx / 10) % 10 + '0';
  59. conv[5] = (xx) % 10 + '0';
  60. conv[6] = 0;
  61. return conv;
  62. }
  63. //// Convert float to rj string with 123.45 format
  64. char *ftostr32ns(const float &x) {
  65. long xx = abs(x);
  66. conv[0] = xx >= 10000 ? (xx / 10000) % 10 + '0' : ' ';
  67. conv[1] = xx >= 1000 ? (xx / 1000) % 10 + '0' : ' ';
  68. conv[2] = xx >= 100 ? (xx / 100) % 10 + '0' : '0';
  69. conv[3] = '.';
  70. conv[4] = (xx / 10) % 10 + '0';
  71. conv[5] = xx % 10 + '0';
  72. return conv;
  73. }
  74. // Convert float to string with 1.234 format
  75. char *ftostr43(const float &x, uint8_t offset)
  76. {
  77. const size_t maxOffset = sizeof(conv)/sizeof(conv[0]) - 6;
  78. if (offset>maxOffset) offset = maxOffset;
  79. long xx = x * 1000;
  80. if (xx >= 0)
  81. conv[offset] = (xx / 1000) % 10 + '0';
  82. else
  83. conv[offset] = '-';
  84. xx = abs(xx);
  85. conv[offset + 1] = '.';
  86. conv[offset + 2] = (xx / 100) % 10 + '0';
  87. conv[offset + 3] = (xx / 10) % 10 + '0';
  88. conv[offset + 4] = (xx) % 10 + '0';
  89. conv[offset + 5] = 0;
  90. return conv;
  91. }
  92. //Float to string with 1.23 format
  93. char *ftostr12ns(const float &x)
  94. {
  95. long xx = x * 100;
  96. xx = abs(xx);
  97. conv[0] = (xx / 100) % 10 + '0';
  98. conv[1] = '.';
  99. conv[2] = (xx / 10) % 10 + '0';
  100. conv[3] = (xx) % 10 + '0';
  101. conv[4] = 0;
  102. return conv;
  103. }
  104. //Float to string with 1.234 format
  105. char *ftostr13ns(const float &x)
  106. {
  107. long xx = x * 1000;
  108. if (xx >= 0)
  109. conv[0] = ' ';
  110. else
  111. conv[0] = '-';
  112. xx = abs(xx);
  113. conv[1] = (xx / 1000) % 10 + '0';
  114. conv[2] = '.';
  115. conv[3] = (xx / 100) % 10 + '0';
  116. conv[4] = (xx / 10) % 10 + '0';
  117. conv[5] = (xx) % 10 + '0';
  118. conv[6] = 0;
  119. return conv;
  120. }
  121. // convert float to space-padded string with -_23.4_ format
  122. char *ftostr32sp(const float &x) {
  123. long xx = abs(x * 100);
  124. uint8_t dig;
  125. if (x < 0) { // negative val = -_0
  126. conv[0] = '-';
  127. dig = (xx / 1000) % 10;
  128. conv[1] = dig ? '0' + dig : ' ';
  129. }
  130. else { // positive val = __0
  131. dig = (xx / 10000) % 10;
  132. if (dig) {
  133. conv[0] = '0' + dig;
  134. conv[1] = '0' + (xx / 1000) % 10;
  135. }
  136. else {
  137. conv[0] = ' ';
  138. dig = (xx / 1000) % 10;
  139. conv[1] = dig ? '0' + dig : ' ';
  140. }
  141. }
  142. conv[2] = '0' + (xx / 100) % 10; // lsd always
  143. dig = xx % 10;
  144. if (dig) { // 2 decimal places
  145. conv[5] = '0' + dig;
  146. conv[4] = '0' + (xx / 10) % 10;
  147. conv[3] = '.';
  148. }
  149. else { // 1 or 0 decimal place
  150. dig = (xx / 10) % 10;
  151. if (dig) {
  152. conv[4] = '0' + dig;
  153. conv[3] = '.';
  154. }
  155. else {
  156. conv[3] = conv[4] = ' ';
  157. }
  158. conv[5] = ' ';
  159. }
  160. conv[6] = '\0';
  161. return conv;
  162. }
  163. char *itostr31(const int &xx)
  164. {
  165. conv[0] = (xx >= 0) ? '+' : '-';
  166. conv[1] = (xx / 1000) % 10 + '0';
  167. conv[2] = (xx / 100) % 10 + '0';
  168. conv[3] = (xx / 10) % 10 + '0';
  169. conv[4] = '.';
  170. conv[5] = (xx) % 10 + '0';
  171. conv[6] = 0;
  172. return conv;
  173. }
  174. // Convert int to rj string with 123 or -12 format
  175. char *itostr3(const int &x)
  176. {
  177. int xx = x;
  178. if (xx < 0) {
  179. conv[0] = '-';
  180. xx = -xx;
  181. } else if (xx >= 100)
  182. conv[0] = (xx / 100) % 10 + '0';
  183. else
  184. conv[0] = ' ';
  185. if (xx >= 10)
  186. conv[1] = (xx / 10) % 10 + '0';
  187. else
  188. conv[1] = ' ';
  189. conv[2] = (xx) % 10 + '0';
  190. conv[3] = 0;
  191. return conv;
  192. }
  193. // Convert int to lj string with 123 format
  194. char *itostr3left(const int &xx)
  195. {
  196. if (xx >= 100)
  197. {
  198. conv[0] = (xx / 100) % 10 + '0';
  199. conv[1] = (xx / 10) % 10 + '0';
  200. conv[2] = (xx) % 10 + '0';
  201. conv[3] = 0;
  202. }
  203. else if (xx >= 10)
  204. {
  205. conv[0] = (xx / 10) % 10 + '0';
  206. conv[1] = (xx) % 10 + '0';
  207. conv[2] = 0;
  208. }
  209. else
  210. {
  211. conv[0] = (xx) % 10 + '0';
  212. conv[1] = 0;
  213. }
  214. return conv;
  215. }
  216. // Convert int to rj string with 1234 format
  217. char *itostr4(const int &xx) {
  218. conv[0] = xx >= 1000 ? (xx / 1000) % 10 + '0' : ' ';
  219. conv[1] = xx >= 100 ? (xx / 100) % 10 + '0' : ' ';
  220. conv[2] = xx >= 10 ? (xx / 10) % 10 + '0' : ' ';
  221. conv[3] = xx % 10 + '0';
  222. conv[4] = 0;
  223. return conv;
  224. }
  225. // Convert float to rj string with 12345 format
  226. char *ftostr5(const float &x) {
  227. long xx = abs(x);
  228. conv[0] = xx >= 10000 ? (xx / 10000) % 10 + '0' : ' ';
  229. conv[1] = xx >= 1000 ? (xx / 1000) % 10 + '0' : ' ';
  230. conv[2] = xx >= 100 ? (xx / 100) % 10 + '0' : ' ';
  231. conv[3] = xx >= 10 ? (xx / 10) % 10 + '0' : ' ';
  232. conv[4] = xx % 10 + '0';
  233. conv[5] = 0;
  234. return conv;
  235. }
  236. // Convert float to string with +1234.5 format
  237. char *ftostr51(const float &x)
  238. {
  239. long xx = x * 10;
  240. conv[0] = (xx >= 0) ? '+' : '-';
  241. xx = abs(xx);
  242. conv[1] = (xx / 10000) % 10 + '0';
  243. conv[2] = (xx / 1000) % 10 + '0';
  244. conv[3] = (xx / 100) % 10 + '0';
  245. conv[4] = (xx / 10) % 10 + '0';
  246. conv[5] = '.';
  247. conv[6] = (xx) % 10 + '0';
  248. conv[7] = 0;
  249. return conv;
  250. }
  251. // Convert float to string with +123.45 format
  252. char *ftostr52(const float &x)
  253. {
  254. long xx = x * 100;
  255. conv[0] = (xx >= 0) ? '+' : '-';
  256. xx = abs(xx);
  257. conv[1] = (xx / 10000) % 10 + '0';
  258. conv[2] = (xx / 1000) % 10 + '0';
  259. conv[3] = (xx / 100) % 10 + '0';
  260. conv[4] = '.';
  261. conv[5] = (xx / 10) % 10 + '0';
  262. conv[6] = (xx) % 10 + '0';
  263. conv[7] = 0;
  264. return conv;
  265. }