/
stdlib.c
executable file
·81 lines (72 loc) · 1.21 KB
/
stdlib.c
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Function declerations
*/
char *num_to_str(int number, int base);
char num_to_char(int num, int base);
int strlen(const char *str);
void reverse(char str[]);
/*
* maximum number can be (-) signed long
*/
#define MAXLEN 12
char buff[MAXLEN]; //
/*
* strlen()
* calculates length of given string
*/
int strlen(const char *str)
{
int i;
for(i=0;str[i] != '\0';i++);
return i;
}
/*
* reverse()
* reverses the string, like 1234567 to 7654321
*/
void reverse(char str[])
{
int len = strlen(str), i;
char tmp;
for(i = 0;i < len/2;i++)
{
tmp = str[len-i-1];
str[len-i-1] = str[i];
str[i] = tmp;
}
}
/*
* num_to_str()
* FIXME Bloody BUG while converting to Octal !
*/
char *num_to_str(int number, int base)
{
char *ptr = buff;
int kalan, i, tmp_num = number;
for(i = 0;i < MAXLEN;i++)
{
kalan = tmp_num % base;
if(tmp_num <= 0){
*ptr = '\0';
reverse(buff);
return buff;
}
*ptr++ = num_to_char(kalan, base);
tmp_num /= base;
}
return buff; // should never reach here
}
char num_to_char(int num, int base)
{
switch(base){
case 16:
if(num >= 10 && num <= 15)
return num + 55;
case 10:
case 8:
if(num >= 0 && num <= 9)
return num + 48;
default:
return '\0';
}
}