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