-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask3.c
executable file
·104 lines (94 loc) · 1.96 KB
/
Task3.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
#include "main.h"
#include <stdlib.h>
/**
* print_unsigned - helper function to print unsigned integer
* @num: the number to be printed as unsigned integer
* Return: number of integers printed
*/
int print_unsigned(unsigned int num)
{
int index, count;
char buffer[20];/*enough for 32-bit unsigned int*/
if (num == 0)
{
return (_putchar('0'));
}
index = 0;
while (num > 0)
{
/*convert the last digit to its character representation*/
buffer[index++] = '0' + (num % 10);
num /= 10;/*move to the next digit*/
}
count = 0;
while (index > 0)
{
count += _putchar(buffer[--index]);
}
return (count);
}
/**
* print_octal - helper function to convert unsigned int
* to its octal representation
* @num: number to be converted
* Return: count of numbers printed
*/
int print_octal(unsigned int num)
{
int index, count;
char buffer[12];
if (num == 0)
{
return (_putchar('0'));
}
index = 0;
while (num > 0)
{
/*take the last 3bits(octal) and convert to its character represenation*/
buffer[index++] = '0' + (num & 7);
/*right shift by 8 bits to get the next octal*/
num >>= 8;
}
count = 0;
while (index > 0)
{
count += _putchar(buffer[--index]);
}
return (count);
}
/**
* print_hexadecimal - helper function to convert unsigned int into hex
* @num: unsigned int to be converted
* @uppercase: a flag to determine whether to print uppercase or lowercase
* Return: a number of character printed
*/
int print_hexadecimal(unsigned int num, int uppercase)
{
int index, digit, count;
char buffer[20];
if (num == 0)
{
return (_putchar('0'));
}
index = 0;
while (num > 0)
{
/*take last 4 bits and convert to its character representation*/
digit = num & 15;
if (digit < 10)
{
buffer[index++] = '0' + digit;
} else
{
buffer[index++] = (uppercase) ? 'A' + digit - 10 : 'a' + digit - 10;
}
/*right shift by 4 bits*/
num >>= 4;
}
count = 0;
while (index > 0)
{
count += _putchar(buffer[--index]);
}
return (count);
}