-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathitoa.h
More file actions
42 lines (38 loc) · 717 Bytes
/
itoa.h
File metadata and controls
42 lines (38 loc) · 717 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef __ITOA_H__
#define __ITOA_H__
/*
* @func: convert int to string, only support decimal.
* @params: val {int}, the value should be converted.
* buf {string}, the result string after converted
* @ret: {int}, the length of result string
*/
int itoa(int val, char* buf){
const unsigned char radix = 10;
char* p = buf;
unsigned int a;
int len;
char* begin;
char tmp;
unsigned int u;
if (val < 0){ // handling the negative.
*p++ = '-';
val = 0 - val;
}
u = (unsigned int)val;
begin = p;
do{
a = u%radix;
u /= radix;
*p++ = a + '0';
}while(u > 0);
len = (int)p - buf;
*p-- = 0;
do{
tmp = *p;
*p = *begin;
*begin = tmp;
--p;
++begin;
}while(begin < p);
}
#endif