conversion.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * WinPR: Windows Portable Runtime
  3. * Data Conversion
  4. *
  5. * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include <winpr/crt.h>
  23. #include <winpr/string.h>
  24. /* Data Conversion: http://msdn.microsoft.com/en-us/library/0heszx3w/ */
  25. #ifndef _WIN32
  26. errno_t _itoa_s(int value, char* buffer, size_t sizeInCharacters, int radix)
  27. {
  28. int length;
  29. length = sprintf_s(NULL, 0, "%d", value);
  30. if (length < 0)
  31. return -1;
  32. if (sizeInCharacters < (size_t)length)
  33. return -1;
  34. sprintf_s(buffer, length + 1, "%d", value);
  35. return 0;
  36. }
  37. errno_t _ltoa_s(long value, char* buffer, size_t sizeInCharacters, int radix)
  38. {
  39. int length;
  40. if (radix == 16) {
  41. length = sprintf_s(NULL, 0, "%lx", value);
  42. }
  43. else {
  44. length = sprintf_s(NULL, 0, "%ld", value);
  45. }
  46. if (sizeInCharacters < length)
  47. return -1;
  48. if (radix == 16) {
  49. sprintf_s(buffer, length + 1, "%lx", value);
  50. }
  51. else {
  52. sprintf_s(buffer, length + 1, "%ld", value);
  53. }
  54. return 0;
  55. }
  56. char* _itoa(int value, char* buffer, int radix)
  57. {
  58. char* p;
  59. unsigned int a;
  60. //int len;
  61. char* b;
  62. char temp;
  63. unsigned int u;
  64. p = buffer;
  65. if (value < 0) {
  66. *p++ = '-';
  67. value = 0 - value;
  68. }
  69. u = (unsigned int)value;
  70. b = p;
  71. do {
  72. a = u % radix;
  73. u /= radix;
  74. *p++ = a + '0';
  75. } while (u > 0);
  76. //len = (int)(p-buffer);
  77. *p-- = 0;
  78. do {
  79. temp = *p;
  80. *p = *b;
  81. *b = temp;
  82. --p;
  83. ++b;
  84. } while (b < p);
  85. return buffer;
  86. }
  87. char* _ultoa(unsigned long value, char* pstring, int radix)
  88. {
  89. char tmp[33] = { 0 };
  90. char* tp = tmp;
  91. long i;
  92. unsigned long v = value;
  93. char* sp;
  94. if (radix > 36 || radix <= 1 || NULL == pstring) {
  95. return 0;
  96. }
  97. while (v || tp == tmp) {
  98. i = v % radix;
  99. v = v / radix;
  100. if (i < 10)
  101. *tp++ = i + '0';
  102. else
  103. *tp++ = i + 'a' - 10;
  104. }
  105. sp = pstring;
  106. while (tp > tmp)
  107. *sp++ = *--tp;
  108. *sp = 0;
  109. return pstring;
  110. }
  111. #endif