-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustom_str.c
90 lines (72 loc) · 1.25 KB
/
custom_str.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
#include "main.h"
/**
* custom_hex - prints hexadecimal value
* of custom characters
* @ascii: ascii value of custom character
*
* Return: number of printed bytes
*/
int custom_hex(int ascii)
{
unsigned int num, digit = 1, b;
char lett, zero = '0';
int l = 0;
char slash = '\\';
char x = 'x';
num = ascii;
b = num;
while (b > 15)
{
digit *= 16;
b /= 16;
}
l += _print_buf(&slash, 1);
l += _print_buf(&x, 1);
for (; digit > 0; digit /= 16)
{
b = (num / digit);
if (b > 9)
lett = (((num / digit) + 7) + '0');
else
lett = (num / digit) + '0';
if (digit == 1 && l == 2)
l += _print_buf(&zero, 1);
l += _print_buf(&lett, 1);
num %= digit;
}
return (l);
}
/**
* custom_str - prints custom character
* to the console
*
* @args: arguments
* @buffer: pointer to character
*
* Return: number of character printed
*/
int custom_str(va_list args, char *buffer)
{
int i = 0, len = 0;
char *null;
char lett;
buffer = va_arg(args, char *);
if (!buffer)
{
null = "(null)";
return (_print_buf(null, 6));
}
for (; buffer[i] != '\0'; i++)
{
if (buffer[i] < 32 || buffer[i] >= 127)
{
len += custom_hex(buffer[i]);
}
else
{
lett = buffer[i];
len += _print_buf(&lett, 1);
}
}
return (len);
}