-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_custom.c
54 lines (51 loc) · 1.15 KB
/
handle_custom.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
#include <stdio.h>
#include <stdarg.h>
#include"main.h"
/**
* handle_binary - handle the b specifier
* @args: va_list of arguments
* @count: pointer to character
*/
void handle_binary(va_list args, int *count)
{
unsigned int num = va_arg(args, unsigned int);
int i, num_bits = 0;
unsigned int temp = num;
if (num == 0)
{
putchar('0');
(*count)++;
return;
}
while (temp > 0)
{
temp /= 2;
num_bits++;
}
for (i = num_bits - 1; i >= 0; i--)
{
putchar(((num >> i) & 1) + '0');
(*count)++;
}
}
/**
* custom_string - Handles the %S specifier
* @args: The va_list containing the arguments
* @count: Pointer to the character count
*/
void custom_string(va_list args, int *count) {
const char *str = va_arg(args, const char *);
while (*str) {
if (*str >= 32 && *str < 127) {
putchar(*str);
(*count)++;
} else {
(*count) += 4;
putchar('\\');
putchar('x');
putchar((*str >> 4) < 10 ? (*str >> 4) + '0' : (*str >> 4) - 10 + 'A');
putchar((*str & 0x0F) < 10 ? (*str & 0x0F) + '0' : (*str & 0x0F) - 10 + 'A');
}
str++;
}
}