Itoa.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "EmptyHeader.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. // Fast itoa from http://www.jb.man.ac.uk/~slowe/cpp/itoa.html for Linux since it seems like Linux doesn't support this function.
  15. // I modified it to remove the std dependencies.
  16. char* Itoa( int value, char* result, int base )
  17. {
  18. // check that the base if valid
  19. if (base < 2 || base > 16) { *result = 0; return result; }
  20. char* out = result;
  21. int quotient = value;
  22. int absQModB;
  23. do {
  24. // KevinJ - get rid of this dependency
  25. //*out = "0123456789abcdef"[ std::abs( quotient % base ) ];
  26. absQModB=quotient % base;
  27. if (absQModB < 0)
  28. absQModB=-absQModB;
  29. *out = "0123456789abcdef"[ absQModB ];
  30. ++out;
  31. quotient /= base;
  32. } while ( quotient );
  33. // Only apply negative sign for base 10
  34. if ( value < 0 && base == 10) *out++ = '-';
  35. // KevinJ - get rid of this dependency
  36. // std::reverse( result, out );
  37. *out = 0;
  38. // KevinJ - My own reverse code
  39. char *start = result;
  40. char temp;
  41. out--;
  42. while (start < out)
  43. {
  44. temp=*start;
  45. *start=*out;
  46. *out=temp;
  47. start++;
  48. out--;
  49. }
  50. return result;
  51. }
  52. #ifdef __cplusplus
  53. }
  54. #endif
粤ICP备19079148号