-
Notifications
You must be signed in to change notification settings - Fork 3
/
3_print_hex.c
71 lines (61 loc) · 1.28 KB
/
3_print_hex.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
/* ***************************************************************************
* Author : Kura Peng (kpeng) <https://github.com/sayakura>
* Created : 2018/10/05
* Updated : 2018/10/05
* ***************************************************************************/
#include <unistd.h>
char *g_base = "0123456789abcdef";
void pc(int c)
{
write(1, &c, 1);
}
int atoi(char *str)
{
int i = 0;
unsigned int result = 0;
while (str[i] >= '0' && str[i] <= '9')
{
result = result * 10 + str[i] - '0';
i++;
}
return (int)(result);
}
void ph(int num)
{
if (num >= 16)
ph(num / 16);
pc(g_base[num % 16]);
}
int main(int ac, char **av)
{
if (ac != 2)
{
pc('\n');
return (0);
}
else
{
int num = atoi(av[1]);
ph(num);
pc('\n');
}
return (0);
}
/*
Assignment name : print_hex
Expected files : print_hex.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program that takes a positive (or zero) number expressed in base 10,
and displays it in base 16 (lowercase letters) followed by a newline.
If the number of parameters is not 1, the program displays a newline.
Examples:
$> ./print_hex "10" | cat -e
a$
$> ./print_hex "255" | cat -e
ff$
$> ./print_hex "5156454" | cat -e
4eae66$
$> ./print_hex | cat -e
$
*/