-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.c
143 lines (135 loc) · 2.01 KB
/
helper.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "main.h"
/**
* str_len - string length
* @s: string
* Return: length
*/
int str_len(char *s)
{
int i = 0;
while (s[i] != '\0')
i++;
return (i);
}
/**
* print_int - prints digit
* @n: number to be printed
* Return: length
*/
int print_int(int n)
{
int i = 0;
unsigned int num;
num = n;
if (n < 0)
{
i += _putchar('-');
num = -num;
}
if (num == 0)
{
i += _putchar('0');
return (i);
}
if (num / 10)
i += print_int(num / 10);
i += _putchar(num % 10 + '0');
return (i);
}
/**
* hex_low - prints number in hexadecimal in lower case
* @n: number
* Return: length
*/
int hex_low(unsigned long int n)
{
long int i, *arr, count = 0;
unsigned long int num;
num = n;
while (n / 16 != 0)
{
n /= 16;
count++;
}
count++;
arr = malloc(sizeof(long int) * count);
if (arr == NULL)
return (0);
for (i = 0; i < count; i++)
{
arr[i] = num % 16;
num = num / 16;
}
for (i = count - 1; i >= 0; i--)
{
if (arr[i] > 9)
arr[i] = arr[i] + 39;
_putchar(arr[i] + '0');
}
free(arr);
return (count);
}
/**
* binary - prints number in binary
* @n: number
* Return: length
*/
int binary(unsigned int n)
{
int i, *arr, count = 0;
unsigned int num;
num = n;
while (n / 2 != 0)
{
n /= 2;
count++;
}
count++;
arr = malloc(sizeof(int) * count);
if (arr == NULL)
return (0);
for (i = 0; i < count; i++)
{
arr[i] = num % 2;
num = num / 2;
}
for (i = count - 1; i >= 0; i--)
{
_putchar(arr[i] + '0');
}
free(arr);
return (count);
}
/**
* hex_upper - prints number in hexadecimal in lower case
* @n: number
* Return: length
*/
int hex_upper(unsigned long int n)
{
int long i, *arr, count = 0;
unsigned long int num;
num = n;
while (n / 16 != 0)
{
n /= 16;
count++;
}
count++;
arr = malloc(sizeof(long int) * count);
if (arr == NULL)
return (0);
for (i = 0; i < count; i++)
{
arr[i] = num % 16;
num = num / 16;
}
for (i = count - 1; i >= 0; i--)
{
if (arr[i] > 9)
arr[i] = arr[i] + 7;
_putchar(arr[i] + '0');
}
free(arr);
return (count);
}