Skip to content

Commit df8c111

Browse files
add atoi()
1 parent cae627a commit df8c111

File tree

2 files changed

+148
-24
lines changed

2 files changed

+148
-24
lines changed
Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,97 @@
1-
## Safe Memcpy that Handles Overlapped Src & Dest (Memmove)
2-
#### ***Usage***
1+
## Atoi that takes different bases input
2+
### ***Usage***
33
```
44
make
55
./atoi
66
```
77

8-
#### ***Notes***
8+
### ***Notes***
99

1010

1111

12-
#### ***Improvements***
12+
### ***Improvements***
1313

1414

1515

16-
#### ***Code***
17-
###### itoa.c
16+
### ***Code***
1817
```c
18+
#include <stdlib.h>
19+
#include <string.h>
20+
#include <stdio.h>
21+
#include <limits.h>
1922

23+
int my_atoi(char* num, int size, int base) {
24+
int i, ret = 0;
25+
int neg = 0;
26+
27+
if (!num || size == 0)
28+
return ret;
29+
30+
// take care of negative sign if any
31+
if (num[0] == '-') {
32+
neg = 1;
33+
i = 1;
34+
} else {
35+
i = 0;
36+
}
37+
38+
// take care of different bases
39+
for (i; i < size; i++) {
40+
ret *= base;
41+
if (base != 16)
42+
ret += num[i] - '0';
43+
else { // special handling for Hex numbers
44+
if (num[i] >= 'a' && num[i] <= 'f')
45+
ret += num[i] - 'a' + 10;
46+
else if (num[i] >= 'A' && num[i] <= 'F')
47+
ret += num[i] - 'A' + 10;
48+
else if (num[i] >= '0' && num[i] <= '9')
49+
ret += num[i] - '0';
50+
}
51+
}
52+
53+
return neg ? -ret : ret;
54+
}
55+
56+
int main(int argc, char **argv) {
57+
int ret;
58+
59+
char* test_base10 = "123456";
60+
printf("my: %d expect: %d", my_atoi(test_base10, strlen(test_base10), 10), atoi(test_base10));
61+
printf("\n==============\n");
62+
63+
test_base10 = "-123456";
64+
printf("my: %d expect: %d ", my_atoi(test_base10, strlen(test_base10), 10), atoi(test_base10));
65+
printf("\n==============\n");
66+
67+
char *test_base2 = "1000";
68+
printf("%d ", my_atoi(test_base2, strlen(test_base2), 2));
69+
printf("\n==============\n");
70+
71+
test_base2 = "111";
72+
printf("%d ", my_atoi(test_base2, strlen(test_base2), 2));
73+
printf("\n==============\n");
74+
75+
char *test_base16 = "1a";
76+
printf("%d ", my_atoi(test_base16, strlen(test_base16), 16));
77+
printf("\n==============\n");
78+
79+
test_base16 = "100";
80+
printf("%d ", my_atoi(test_base16, strlen(test_base16), 16));
81+
printf("\n==============\n");
82+
83+
test_base16 = "ff";
84+
printf("%d ", my_atoi(test_base16, strlen(test_base16), 16));
85+
printf("\n==============\n");
86+
87+
test_base16 = "1A";
88+
printf("%d ", my_atoi(test_base16, strlen(test_base16), 16));
89+
printf("\n==============\n");
90+
91+
test_base16 = "FF";
92+
printf("%d ", my_atoi(test_base16, strlen(test_base16), 16));
93+
printf("\n==============\n");
94+
return 0;
95+
}
2096
```
2197

Data_Struct_Implementation/atoi/atoi.c

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,76 @@
33
#include <stdio.h>
44
#include <limits.h>
55

6+
int my_atoi(char* num, int size, int base) {
7+
int i, ret = 0;
8+
int neg = 0;
9+
10+
if (!num || size == 0)
11+
return ret;
12+
13+
// take care of negative sign if any
14+
if (num[0] == '-') {
15+
neg = 1;
16+
i = 1;
17+
} else {
18+
i = 0;
19+
}
20+
21+
// take care of different bases
22+
for (i; i < size; i++) {
23+
ret *= base;
24+
if (base != 16)
25+
ret += num[i] - '0';
26+
else { // special handling for Hex numbers
27+
if (num[i] >= 'a' && num[i] <= 'f')
28+
ret += num[i] - 'a' + 10;
29+
else if (num[i] >= 'A' && num[i] <= 'F')
30+
ret += num[i] - 'A' + 10;
31+
else if (num[i] >= '0' && num[i] <= '9')
32+
ret += num[i] - '0';
33+
}
34+
}
35+
36+
return neg ? -ret : ret;
37+
}
638

739
int main(int argc, char **argv) {
8-
int i;
9-
char buffer[64];
10-
int buf_s = 64;
11-
12-
int test_base10[6] = {0, -10, 12345, -12345, -32768, INT_MAX};
13-
memset(buffer, 0, buf_s);
14-
for (i=0; i<6; i++)
15-
printf("%s ", itoa(test_base10[i], buffer, 10));
40+
int ret;
41+
42+
char* test_base10 = "123456";
43+
printf("my: %d expect: %d", my_atoi(test_base10, strlen(test_base10), 10), atoi(test_base10));
1644
printf("\n==============\n");
1745

18-
int test_base2[6] = {0, -16, 12345, -12345, -32768, INT_MAX};
19-
memset(buffer, 0, buf_s);
20-
for (i=0; i<6; i++)
21-
printf("%s ", itoa(test_base2[i], buffer, 2));
22-
printf("\n==============\n");
23-
24-
int test_base16[6] = {0, -10, 0x1234abcd, -12345, -32768, INT_MAX};
25-
memset(buffer, 0, buf_s);
26-
for (i=0; i<6; i++)
27-
printf("%s ", itoa(test_base16[i], buffer, 16));
46+
test_base10 = "-123456";
47+
printf("my: %d expect: %d ", my_atoi(test_base10, strlen(test_base10), 10), atoi(test_base10));
2848
printf("\n==============\n");
49+
50+
char *test_base2 = "1000";
51+
printf("%d ", my_atoi(test_base2, strlen(test_base2), 2));
52+
printf("\n==============\n");
53+
54+
test_base2 = "111";
55+
printf("%d ", my_atoi(test_base2, strlen(test_base2), 2));
56+
printf("\n==============\n");
57+
58+
char *test_base16 = "1a";
59+
printf("%d ", my_atoi(test_base16, strlen(test_base16), 16));
60+
printf("\n==============\n");
61+
62+
test_base16 = "100";
63+
printf("%d ", my_atoi(test_base16, strlen(test_base16), 16));
64+
printf("\n==============\n");
65+
66+
test_base16 = "ff";
67+
printf("%d ", my_atoi(test_base16, strlen(test_base16), 16));
68+
printf("\n==============\n");
69+
70+
test_base16 = "1A";
71+
printf("%d ", my_atoi(test_base16, strlen(test_base16), 16));
72+
printf("\n==============\n");
73+
74+
test_base16 = "FF";
75+
printf("%d ", my_atoi(test_base16, strlen(test_base16), 16));
76+
printf("\n==============\n");
2977
return 0;
3078
}

0 commit comments

Comments
 (0)