]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/ustrfmt.c
ICU-551.51.4.tar.gz
[apple/icu.git] / icuSources / common / ustrfmt.c
1 /*
2 **********************************************************************
3 * Copyright (C) 2001-2006, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 */
7
8 #include "cstring.h"
9 #include "ustrfmt.h"
10
11
12 /***
13 * Fills in a UChar* string with the radix-based representation of a
14 * uint32_t number padded with zeroes to minwidth. The result
15 * will be null terminated if there is room.
16 *
17 * @param buffer UChar buffer to receive result
18 * @param capacity capacity of buffer
19 * @param i the unsigned number to be formatted
20 * @param radix the radix from 2..36
21 * @param minwidth the minimum width. If the result is narrower than
22 * this, '0's will be added on the left. Must be <=
23 * capacity.
24 * @return the length of the result, not including any terminating
25 * null
26 */
27 U_CAPI int32_t U_EXPORT2
28 uprv_itou (UChar * buffer, int32_t capacity,
29 uint32_t i, uint32_t radix, int32_t minwidth)
30 {
31 int32_t length = 0;
32 int digit;
33 int32_t j;
34 UChar temp;
35
36 do{
37 digit = (int)(i % radix);
38 buffer[length++]=(UChar)(digit<=9?(0x0030+digit):(0x0030+digit+7));
39 i=i/radix;
40 } while(i && length<capacity);
41
42 while (length < minwidth){
43 buffer[length++] = (UChar) 0x0030;/*zero padding */
44 }
45 /* null terminate the buffer */
46 if(length<capacity){
47 buffer[length] = (UChar) 0x0000;
48 }
49
50 /* Reverses the string */
51 for (j = 0; j < (length / 2); j++){
52 temp = buffer[(length-1) - j];
53 buffer[(length-1) - j] = buffer[j];
54 buffer[j] = temp;
55 }
56 return length;
57 }