home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / wtox.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  3KB  |  116 lines

  1. /***
  2. *wtox.c - _wtoi and _wtol conversion
  3. *
  4. *       Copyright (c) 1993-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Converts a wide character string into an int or long.
  8. *
  9. *******************************************************************************/
  10.  
  11.  
  12. #include <windows.h>
  13. #include <stdlib.h>
  14.  
  15. #define INT_SIZE_LENGTH   20
  16. #define LONG_SIZE_LENGTH  40
  17. #define I64_SIZE_LENGTH     80
  18.  
  19. /***
  20. *long _wtol(wchar_t *nptr) - Convert wide string to long
  21. *
  22. *Purpose:
  23. *       Converts wide string pointed to by nptr to binary.
  24. *       Overflow is not detected.  Because of this, we can just use
  25. *       atol().
  26. *
  27. *Entry:
  28. *       nptr = ptr to wide string to convert
  29. *
  30. *Exit:
  31. *       return long value of the string
  32. *
  33. *Exceptions:
  34. *       None - overflow is not detected.
  35. *
  36. *******************************************************************************/
  37.  
  38. long __cdecl _wtol(
  39.         const wchar_t *nptr
  40.         )
  41. {
  42.         char astring[INT_SIZE_LENGTH];
  43.  
  44.         WideCharToMultiByte (CP_ACP, 0, nptr, -1,
  45.                             astring, INT_SIZE_LENGTH, NULL, NULL);
  46.  
  47.         return (atol(astring));
  48. }
  49.  
  50. /***
  51. *int _wtoi(wchar_t *nptr) - Convert wide string to int
  52. *
  53. *Purpose:
  54. *       Converts wide string pointed to by nptr to binary.
  55. *       Overflow is not detected.  Because of this, we can just use
  56. *       atol().
  57. *
  58. *Entry:
  59. *       nptr = ptr to wide string to convert
  60. *
  61. *Exit:
  62. *       return int value of the string
  63. *
  64. *Exceptions:
  65. *       None - overflow is not detected.
  66. *
  67. *******************************************************************************/
  68.  
  69. int __cdecl _wtoi(
  70.         const wchar_t *nptr
  71.         )
  72. {
  73.         char astring[INT_SIZE_LENGTH];
  74.  
  75.         WideCharToMultiByte (CP_ACP, 0, nptr, -1,
  76.                             astring, INT_SIZE_LENGTH, NULL, NULL);
  77.  
  78.         return ((int)atol(astring));
  79. }
  80.  
  81. #ifndef _NO_INT64
  82.  
  83. /***
  84. *__int64 _wtoi64(wchar_t *nptr) - Convert wide string to __int64
  85. *
  86. *Purpose:
  87. *       Converts wide string pointed to by nptr to binary.
  88. *       Overflow is not detected.  Because of this, we can just use
  89. *       _atoi64().
  90. *
  91. *Entry:
  92. *       nptr = ptr to wide string to convert
  93. *
  94. *Exit:
  95. *       return __int64 value of the string
  96. *
  97. *Exceptions:
  98. *       None - overflow is not detected.
  99. *
  100. *******************************************************************************/
  101.  
  102. __int64 __cdecl _wtoi64(
  103.         const wchar_t *nptr
  104.         )
  105. {
  106.         char astring[I64_SIZE_LENGTH];
  107.  
  108.         WideCharToMultiByte (CP_ACP, 0, nptr, -1,
  109.                             astring, I64_SIZE_LENGTH, NULL, NULL);
  110.  
  111.         return (_atoi64(astring));
  112. }
  113.  
  114. #endif  /* _NO_INT64 */
  115.  
  116.