num2str.c 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * NUM2STR - Functions to handle the conversion of numeric vales to strings.
  3. *
  4. * @author Neven Boyanov
  5. *
  6. * This is part of the Tinusaur/TinyAVRLib project.
  7. *
  8. * Copyright (c) 2017 Neven Boyanov, The Tinusaur Team. All Rights Reserved.
  9. * Distributed as open source software under MIT License, see LICENSE.txt file.
  10. * Retain in your source code the link http://tinusaur.org to the Tinusaur project.
  11. *
  12. * Source code available at: https://bitbucket.org/tinusaur/tinyavrlib
  13. *
  14. */
  15. // ============================================================================
  16. #include "num2str.h"
  17. // ----------------------------------------------------------------------------
  18. // NOTE: The resulting ASCII text should not be terminated with '\0' in the
  19. // functions below because the buffer might be part of a larger text.
  20. // ----------------------------------------------------------------------------
  21. uint8_t usint2decascii(uint16_t num, char *buffer) {
  22. const unsigned short powers[] = { 10000u, 1000u, 100u, 10u, 1u }; // The "const unsigned short" combination gives shortest code.
  23. char digit; // "digit" is stored in a char array, so it should be of type char.
  24. uint8_t digits = USINT2DECASCII_MAX_DIGITS - 1;
  25. for (uint8_t pos = 0; pos < 5; pos++) { // "pos" is index in array.
  26. digit = 0;
  27. while (num >= powers[pos]) {
  28. digit++;
  29. num -= powers[pos];
  30. }
  31. // Fixed width, space (or anything else) padded result, digits offset.
  32. // Note: Determines the offset of the first significant digit.
  33. // Note: Could be used for variable width, not padded, left aligned result.
  34. if (digits == USINT2DECASCII_MAX_DIGITS - 1) {
  35. if (digit == 0) {
  36. if (pos < USINT2DECASCII_MAX_DIGITS - 1) // Check position, so single "0" will be handled properly.
  37. digit = 0;//!!! -16; // Use: "-16" for space (' '), "-3" for dash/minus ('-'), "0" for zero ('0'), etc. ...
  38. } else {
  39. digits = pos;
  40. }
  41. }
  42. buffer[pos] = digit + '0'; // Convert to ASCII
  43. }
  44. return digits;
  45. }
  46. // ----------------------------------------------------------------------------
  47. uint8_t usint2hexascii(uint16_t num, char *buffer) {
  48. for (int8_t pos = USINT2HEXASCII_MAX_DIGITS - 1; pos >= 0 ; pos--) { // "pos" is index in an array.
  49. char digit = num & 0x000f;
  50. if (digit <= 9) {
  51. buffer[pos] = digit + '0'; // Convert to ASCII
  52. } else {
  53. buffer[pos] = digit + 'A' - 10; // Convert to ASCII
  54. }
  55. num = num >> 4;
  56. }
  57. return 4;
  58. }
  59. // ----------------------------------------------------------------------------
  60. // NOTE: The buffer should be always at least MAX_DIGITS in length - the function works with 16-bit numbers.
  61. uint8_t usint2binascii(uint16_t num, char *buffer) {
  62. uint16_t power = 0x8000; // This is the 1000 0000 0000 0000 binary number.
  63. char digit; // "digit" is stored in a char array, so it should be of type char.
  64. uint8_t digits = USINT2BINASCII_MAX_DIGITS - 1;
  65. for (uint8_t pos = 0; pos < USINT2BINASCII_MAX_DIGITS; pos++) { // "pos" is index in an array.
  66. digit = 0;
  67. if (num >= power) {
  68. digit++;
  69. num -= power;
  70. }
  71. // Fixed width, space ('0', or anything else) padded result, digits offset.
  72. // Note: Determines the offset of the first significant digit.
  73. // Note: Could be used for variable width, not padded, left aligned result.
  74. if (digits == USINT2BINASCII_MAX_DIGITS - 1) {
  75. if (digit == 0) {
  76. if (pos < USINT2BINASCII_MAX_DIGITS - 1) // Check position, so single "0" will be handled properly.
  77. digit = 0; // Use: "-16" for space (' '), "-3" for dash/minus ('-'), "0" for zero ('0'), etc.
  78. } else {
  79. digits = pos;
  80. }
  81. }
  82. buffer[pos] = digit + '0'; // Convert to ASCII
  83. power = power >> 1;
  84. }
  85. return digits;
  86. }
  87. // ============================================================================