9a378aa818e91011d0f8fb124056d2468c04644a
[reactos.git] / reactos / lib / crt / stdlib / itow.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/msvcrt/stdlib/itow.c
6 * PURPOSE: converts a integer to wchar_t
7 * PROGRAMER:
8 * UPDATE HISTORY:
9 * 1995: Created
10 * 1998: Added ltoa Boudewijn Dekker
11 * 2000: derived from ./itoa.c by ea
12 */
13 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
14
15 #include <precomp.h>
16
17 /*
18 * @implemented
19 * from wine cvs 2006-05-21
20 */
21 wchar_t* _itow(int value, wchar_t* string, int radix)
22 {
23 return _ltow(value, string, radix);
24 }
25
26 /*
27 * @implemented
28 * from wine cvs 2006-05-21
29 */
30 wchar_t* _ltow(long value, wchar_t* string, int radix)
31 {
32 unsigned long val;
33 int negative;
34 WCHAR buffer[33];
35 PWCHAR pos;
36 WCHAR digit;
37
38 if (value < 0 && radix == 10) {
39 negative = 1;
40 val = -value;
41 } else {
42 negative = 0;
43 val = value;
44 } /* if */
45
46 pos = &buffer[32];
47 *pos = '\0';
48
49 do {
50 digit = val % radix;
51 val = val / radix;
52 if (digit < 10) {
53 *--pos = '0' + digit;
54 } else {
55 *--pos = 'a' + digit - 10;
56 } /* if */
57 } while (val != 0L);
58
59 if (negative) {
60 *--pos = '-';
61 } /* if */
62
63 if (str != NULL) {
64 memcpy(string, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
65 } /* if */
66 return string;
67 }
68
69 /*
70 * @implemented
71 * from wine cvs 2006-05-21
72 */
73 wchar_t* _ultow(unsigned long value, wchar_t* string, int radix)
74 {
75 WCHAR buffer[33];
76 PWCHAR pos;
77 WCHAR digit;
78
79 pos = &buffer[32];
80 *pos = '\0';
81
82 do {
83 digit = value % radix;
84 value = value / radix;
85 if (digit < 10) {
86 *--pos = '0' + digit;
87 } else {
88 *--pos = 'a' + digit - 10;
89 } /* if */
90 } while (value != 0L);
91
92 if (string != NULL) {
93 memcpy(string, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
94 } /* if */
95 return string;
96 }