-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchars2.c
67 lines (62 loc) · 1.3 KB
/
chars2.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
#include "main.h"
#include <stdlib.h>
/**
* print_S - prints a string with \x + hexa Upper
* of non printable characters
*
* @args: list of variable arguments
*
* @buffer: buffer to store result before printing
*
* Return: the number of printed characters
*/
int print_S(va_list args, char *buffer)
{
int i, j = 0;
char *str = va_arg(args, char *);
int count = 0;
for (i = 0; i < _strlen(str); i++)
{
if (str[i] < 32 || str[i] >= 127)
{
count += flush_buffer(buffer);
j = 0;
count += _putchar(92);
count += _putchar('x');
if (str[i] < 16)
{
count += _putchar(48);
count += print_hexa_up2(str[i], buffer);
}
else
count += print_hexa_up2(str[i], buffer);
}
else
{
buffer[j] = str[i];
j++;
}
}
count += flush_buffer(buffer);
return (count);
}
/**
* print_hexa_up2 - print an integer in hexadecimal base in uppercase
*
* @dec: given int to convert
*
* @buffer: store the converted number in hexadecimal base
*
* Return: number of digits of the converted integer
*/
int print_hexa_up2(unsigned long dec, char *buffer)
{
char *ptr, *rev_ptr;
if (dec < 10)
return (_putchar(dec + '0'));
if (dec > 9 && dec < 16)
return (_putchar(dec + 55));
rev_ptr = store_base_reverse(dec, 16, 1, buffer);
ptr = rev_str(rev_ptr);
return (flush_buffer(ptr));
}