itoa.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2017 Techie Delight
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included
  11. * in all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  17. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  18. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. */
  20. /* Originally released by techiedelight.com
  21. * (http://www.techiedelight.com/implement-itoa-function-in-c/) under the
  22. * MIT license. */
  23. #include "itoa.h"
  24. static void swap(char *x, char *y) {
  25. char t = *x;
  26. *x = *y;
  27. *y = t;
  28. }
  29. /* function to reverse buffer */
  30. static char* reverse(char *buffer, UA_UInt16 i, UA_UInt16 j) {
  31. while (i < j)
  32. swap(&buffer[i++], &buffer[j--]);
  33. return buffer;
  34. }
  35. /* adapted from http://www.techiedelight.com/implement-itoa-function-in-c/ to use UA_... types */
  36. UA_UInt16 itoaUnsigned(UA_UInt64 value, char* buffer, UA_Byte base) {
  37. /* consider absolute value of number */
  38. UA_UInt64 n = value;
  39. UA_UInt16 i = 0;
  40. while (n) {
  41. UA_UInt64 r = n % base;
  42. if (r >= 10)
  43. buffer[i++] = (char)(65 + (r - 10));
  44. else
  45. buffer[i++] = (char)(48 + r);
  46. n = n / base;
  47. }
  48. /* if number is 0 */
  49. if (i == 0)
  50. buffer[i++] = '0';
  51. buffer[i] = '\0'; /* null terminate string */
  52. i--;
  53. /* reverse the string */
  54. reverse(buffer, 0, i);
  55. i++;
  56. return i;
  57. }
  58. /* adapted from http://www.techiedelight.com/implement-itoa-function-in-c/ to use UA_... types */
  59. UA_UInt16 itoaSigned(UA_Int64 value, char* buffer) {
  60. /* consider absolute value of number */
  61. UA_UInt64 n;
  62. /* Special case for UA_INT64_MIN which can not simply be negated */
  63. /* it will cause a signed integer overflow */
  64. if (value == UA_INT64_MIN) {
  65. n = (UA_UInt64)UA_INT64_MAX + 1;
  66. }
  67. else {
  68. n = (UA_UInt64)value;
  69. if(value < 0){
  70. n = (UA_UInt64)-value;
  71. }
  72. }
  73. UA_UInt16 i = 0;
  74. while (n) {
  75. UA_UInt64 r = n % 10;
  76. if (r >= 10)
  77. buffer[i++] = (char)(65 + (r - 10));
  78. else
  79. buffer[i++] = (char)(48 + r);
  80. n = n / 10;
  81. }
  82. /* if number is 0 */
  83. if (i == 0)
  84. buffer[i++] = '0';
  85. if (value < 0)
  86. buffer[i++] = '-';
  87. buffer[i] = '\0'; /* null terminate string */
  88. i--;
  89. /* reverse the string and return it */
  90. reverse(buffer, 0, i);
  91. i++;
  92. return i;
  93. }